Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (757)
Games in Android Showcase (229)
games submitted by our members
Games in WIP (844)
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  
  Multiple Threads, Multiple Cores  (Read 2330 times)
0 Members and 1 Guest are viewing this topic.
Offline Belenghast

Junior Newbie

« Posted 2007-09-24 06:58:59 »


I have a core 2 duo, which I assume means that there are two CPUs inside my machine, each running at 1.8 GHz.. When my OpenGL Java program runs in a single thread (i.e. just a "normal" program--nothing fancy, everything comes from main()) does it just run on one core? Or does the Java Virtual Machine/Windows Vista cleverly farm out bits of code that are executed simultaneously on both processors in parallel and then magically stick the results back together so that resulting execution looks like it ran on a single processor? (In this case the fact that there are multiple cores is abstracted from the program)


Do I have to programatically spin off another thread outside of main() and explicitly tell that thread to go and run on another core? If this is the case, then is there some Java command/function that selects cores...or does the act of creating another thread give the JVM a chance to work farm it out to cores that it finds aren't so busy? (In this case the program needs to be written to take advantage of multiple cores)

Questions, questions...

Thanks people!

Offline Matzon

JGO Knight

Medals: 19
Projects: 1

I'm gonna wring your pants!

« Reply #1 - Posted 2007-09-24 07:50:16 »

Since the VM uses several threads, the entire VM will use multiple cores. However, your application will ony use 1 core (unless your application is multithreaded).
As for OpenGL code, this is typically single threaded, and indeed the OpenGL driver is single threaded.

Offline askoruli

Senior Newbie

« Reply #2 - Posted 2007-09-24 12:38:23 »

Single threaded applications will only run in a single core. When you have multiple threads the JVM decides where and when they will run, it may use the second core or it may run the thread in the same core. You don't really have any control over that. But in general java will take as much of the system resources as it can get, so if your program has multiple threads that are both using a lot of CPU time it will probably use both cores.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline broumbroum

Junior Devvie

« Reply #3 - Posted 2007-09-24 23:44:58 »

By the way, this has many consequences to the code performances, and as a matter of fact, the ThreadStack will be treated differently on single- and multiple-core systems. For instance, Windows does get much more resources to synchronize on screen with Swing than a Mac OS X does. It's my own experience about that, since I test my code both on ix86 arch and ppc arch. on the other side, single-threaded applications don't provide the best effort on a single-core or non-HyperThreading (unlike ppc ARE obviously 2 logic-channels cpu's) machine because system processor architectures do manage simple functions to be dispatched on both channels (if any available).
There's a conclusion about that speciality on multi-threading, one application will get the best effort only if it launches as more threads the Stack can afford, because the more the code-logic looks like the processor(s) logic, the faster the system may compile and dispatch events to the core(s). It's as easy to understand.
My own experiences proved that I got the best results when targeting my code structure to a multi-threaded style than focusing on single-threaded. Moreover, it looks like the Swing-Timers do not like much to coalesce their events as the actions become slower, and therefore I modified the most of my Timer-Action's to instance a new Thread  when required by sort of long processes. This is know as "coalesceing events".

More concretely, most parts of my code are timed Events, e.g. be that function an usual send-notify a new value to a selected Object :
/** this is the action Thread to make coalesceing */ 
Thread myActionThreadRef;
function Object doActionOff() { // or whatever function you need
          int x = this.positionX;
          sendEventToObject(receiver, x);
          return this.status;
javax.swing.Timer swingTimer = new javax.swing.Timer(10, new ActionListener() { public void actionPerformed(ActionEvent e){
          // here's the trick I mentioned, t is instanciated to free the Swing Timer faster than usual,
          //which has the advantage to manually "coalesce" as Swing does it.
          if(myActionThreadRef instanceof Thread)
                   return; // I don't launch the thread because there's the previous one unterminated
           Thread t = myActionThreadRef = new Thread(new Runnable() { public void run() {
                    Object status = doActionOff();
                    // I never let my objects states un-logged... So here I stick to the rule.
           // maybe set priority to max

This has speeded up to twice the application, something I'm still bringing up for the moment. Roll Eyes
Multi-Threading has many advantages but requires to be experienced with synchronization patterns, as a must-have.  Cheesy

::::... :..... :::::: ;;;:::™ b23:production 2006 GNU/GPL @
on /projects/sf3jswing
Java (1.6u10 plz) Web Start pool
dev' VODcast[/ur
Offline cylab

JGO Kernel

Medals: 180

« Reply #4 - Posted 2007-09-25 08:59:30 »

Multi-Threading has many advantages but requires to be experienced with synchronization patterns...
... and how to avoid them Wink

Mathias - I Know What [you] Did Last Summer!
Offline keldon85

Senior Devvie

Medals: 1

« Reply #5 - Posted 2007-09-25 22:40:06 »

Moderation is the key; it's good to know how multi-core processors can  benefit your program, but it's also good to know when they will not. If you expand the concept of multiple processors to a size of [say] 150, then you have to consider then how they will communicate with each other to perform the task. What you find happens is that your code becomes less optimal as you increase the number of processors due to inter process communication.

There are some processing tasks which can easily be divided across processors and have speed gains in closer proportion to the full potential of the processor, but most tasks are not.

Pages: [1]
  ignore  |  Print  

EgonOlsen (79 views)
2018-06-10 19:43:48

EgonOlsen (59 views)
2018-06-10 19:43:44

EgonOlsen (78 views)
2018-06-10 19:43:20

DesertCoockie (261 views)
2018-05-13 18:23:11

nelsongames (159 views)
2018-04-24 18:15:36

nelsongames (158 views)
2018-04-24 18:14:32

ivj94 (901 views)
2018-03-24 14:47:39

ivj94 (162 views)
2018-03-24 14:46:31

ivj94 (813 views)
2018-03-24 14:43:53

Solater (177 views)
2018-03-17 05:04:08
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05 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‑
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!