Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (492)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (556)
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 1217 times)
0 Members and 1 Guest are viewing this topic.
Offline Belenghast

Junior Newbie





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

Hi,

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)

OR...

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!

/Mark
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 Member





« 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 :
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
/** 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);
          notifyEventToObject(receiver);
          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)
             if(myActionThreadRef.isAlive())
                   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.
                   System.out.println(status);
           }});
           // maybe set priority to max
          t.setMaxPriority(Thread.MAX_PRIORITY);
           t.start();
}});

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 @ http://b23prodtm.webhop.info
on sf.net: /projects/sf3jswing
Java (1.6u10 plz) Web Start pool
dev' VODcast[/ur
Offline cylab

JGO Ninja


Medals: 45



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

Quote
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 Member


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  
 
 
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.

Nickropheliac (15 views)
2014-08-31 22:59:12

TehJavaDev (23 views)
2014-08-28 18:26:30

CopyableCougar4 (33 views)
2014-08-22 19:31:30

atombrot (41 views)
2014-08-19 09:29:53

Tekkerue (40 views)
2014-08-16 06:45:27

Tekkerue (35 views)
2014-08-16 06:22:17

Tekkerue (25 views)
2014-08-16 06:20:21

Tekkerue (37 views)
2014-08-16 06:12:11

Rayexar (72 views)
2014-08-11 02:49:23

BurntPizza (49 views)
2014-08-09 21:09:32
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

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!