Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (120)
games submitted by our members
Games in WIP (577)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Multi core programming with java  (Read 3581 times)
0 Members and 1 Guest are viewing this topic.
Offline Gorgorath

Junior Duke




Java games rock!


« Posted 2006-06-01 19:33:54 »

any tutorials;)
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 817
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #1 - Posted 2006-06-01 20:48:12 »

It's basicly just multi-threading, except that stuff actually happens in parallel.

Any specific questions?


Oh, and the volatile keyword has more importance as there are more caches, hurray

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline harry@dayfamilyweb

Junior Duke





« Reply #2 - Posted 2006-06-01 21:35:45 »

i was told java just atuomaticly did MultiCore/MultiThread
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 817
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #3 - Posted 2006-06-01 21:48:49 »

The only language I know that automaticly multithread code is Fortress, the new Sun language even multithreading for-loops Smiley

But heck no, Java doesn't automaticly multithread code.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline GKW

Senior Duke




Revenge is mine!


« Reply #4 - Posted 2006-06-01 21:59:48 »

The JVM from Sun does spawn multiple threads so in that respect you can say Java is automatically multithreaded.  But you have to make your code multithreaded to take advantage of multiple cores.
Offline Gorgorath

Junior Duke




Java games rock!


« Reply #5 - Posted 2006-06-01 22:43:48 »

in the recent past my game loop was like this:
while(true)

    //handle user input
    //handle AI
    //render
}

right now i'm thinking of something like this
while(true)
{
   //handle input
  //handle AI(every object in his own java.util.lang.thread )
  //render scene( opengl is one thread no MP things here
}

so in step 2 if i extends monsters with their own Thread will the JVM expand this over multiple cores?

Paul
Offline Orangy Tang

JGO Kernel


Medals: 56
Projects: 11


Monkey for a head


« Reply #6 - Posted 2006-06-02 00:03:22 »

right now i'm thinking of something like this
while(true)
{
   //handle input
  //handle AI(every object in his own java.util.lang.thread )
  //render scene( opengl is one thread no MP things here
}
Oh dear goat no! Yes multiple threads will run in parallel if you've got multiple cores, but this is not the way to do it. Firstly you'll cause yourself all sorts of hideous syncronisation issues and race conditions. Fixing that would require considerable amounts of overhead with syncronisation. But the real killer is that context switching from one thread to another is far from free and has quite a bit of overhead. Couple that with the fact that most desktop OS' don't scale very well to lots of threads and you've got something that'll suck up CPU time for very little actual work.

If you want to make optimal use of multiple cores you want a thread per core, then somehow figure out a way to balance all your processing between them. Breaking your loop into small tasks and having a task queue would be a good way, but it'd require quite a lot of changes compared to a normal game loop.

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Offline Ask_Hjorth_Larsen

Junior Duke




Java games rock!


« Reply #7 - Posted 2006-06-02 00:14:07 »

Like Orangy Tang said, you don't want to let your basic game object extend Thread. A thread is a complex thing which uses scores of memory, it has its own stack, for example. You'll probably get OutOfMemoryError in, like, no time. Aside from this, you'll be quite unable to control the update rate since the threads are not ensured equal CPU time.

You may want a handful of threads to do different things, like networking, logic, possible other stuff. But generally it's difficult to work with multiple threads. At least I think so.
Offline harry@dayfamilyweb

Junior Duke





« Reply #8 - Posted 2006-06-02 06:01:46 »

incorect i my game i have about 5 threads running at the same time you get OutOfMemoryError at 70 or somthing (i tried)
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 817
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #9 - Posted 2006-06-02 07:18:23 »

What is incorrect exactly?


Anyway, the above statements are very true, but context-switches aren't *that* bad. You can have a few dozen threads before it becomes noticable in performance.

However, you'll need advanced knowledge of synchronisation and locks. The new java.util.concurrent package might take away most of the obvious bugs, but it's not enough to write your entire game multithreaded.


A great debugging feature when finding deadlocks is Thread.getAllStacktraces() to find out at which lines both Threads are stuck. Unfortunately there is no Thread.getLocks() yet, only Thread.holdsLock(Object obj). These methods might make your multithread-coding life a bit easier, but there is much to learn!

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline CommanderKeith
« Reply #10 - Posted 2006-06-02 08:17:37 »

As was said earlier, all java programs are multi-threaded since the garbage collector among other things runs in its own thread.  Run any java program with the -Xprof option to see just how many threads there are.

Multithreading adds to complexity and can lead to bugs that are random & really hard to track down. 

Also, keep in mind that for games the performance bottleneck is usually in rendering but Java2D takes care of this by using the computer's 2 processors - the CPU & GPU - and you get this by default without even knowing it!  It will be cool when Graphics cards are multi-cored.

Interestingly, on my dual-core 2.8GHz box my 2D game runs 40-100% faster in software mode than in accelerated DirectDraw mode, with the Java2D OpenGL pipeline being slightly slower (but the Mustang J2D OGL pipe line is faster than software mode  Smiley ).

Offline whome

Junior Duke




Carte Noir Java


« Reply #11 - Posted 2006-09-03 09:31:58 »

in the recent past my game loop was like this:
while(true) { 
    //handle user input
    //handle AI
    //render
}

right now i'm thinking of something like this
while(true) {
   //handle input
  //handle AI(every object in his own java.util.lang.thread )
  //render scene( opengl is one thread no MP things here
}

What I find most challenging task is having an efficient sharing of object state between gameThread and rendererThread. And no, I have not implemented such multithreaded game.

gameThread updates state attributes myCar.x, myCar.y, myCar.z, etc... and rendererThread iterates a list of objects and render the scene. Attributes must be synchronized not to be changed in a next gameThread loop while renderer is still using objects. You will ruin per-frame integrity quite easily.

Maybe you could have two or more statepools within each object and threads swap currentRendering and currentUpdate statepools (like doublebuffered thingie for state variables).
Offline Chris61182

Junior Duke





« Reply #12 - Posted 2006-09-19 14:54:28 »

It will be cool when Graphics cards are multi-cored.

Well depending on how you look at it they already are. But in reality lets all hope that they never get multi-cored in the same way that CPUs are, all that would do is reduce the number of pipelines you could have on a single chip or introduce other inefficiencies that the current multi-board setups have.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

theagentd (8 views)
2014-10-25 15:46:29

Longarmx (52 views)
2014-10-17 03:59:02

Norakomi (45 views)
2014-10-16 15:22:06

Norakomi (34 views)
2014-10-16 15:20:20

lcass (38 views)
2014-10-15 16:18:58

TehJavaDev (68 views)
2014-10-14 00:39:48

TehJavaDev (68 views)
2014-10-14 00:35:47

TehJavaDev (60 views)
2014-10-14 00:32:37

BurntPizza (73 views)
2014-10-11 23:24:42

BurntPizza (45 views)
2014-10-11 23:10:45
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!