• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

How does a program use multiple cores?

Is this possible for a java program to do? I know about using the Thread class or Runnable interface but its all managed by the JVMs built in scheduler right? What if i wanted X thread to run on core 0 and Y thread on core 1, how would i program something like that?
 
Any JVM worth anything at all will assign Java threads to system threads, which the OS will then schedule for execution on separate cores as is appropriate. Rarely do you gain very much by trying to tell the OS exactly which cores you want threads on, although in some obscure cases it can help. I doubt this sort of thing is exposed to Java.
 
Right, noted 🙂

So... a program like this guys CPU stress program:

http://weblogs.asp.net/kennykerr/ar...for-your-developer-toolbox_2E002E002E00_.aspx

It would work by having a new thread for what you set it to? Wait never mind, that's pretty much what it says in the description 😳

And this is how you find the number of cores!

http://stackoverflow.com/questions/4759570/finding-number-of-cores-in-java

Its all coming together now :biggrin: Use that method and then spawn a number of threads based on how many cores there are and let the OS's scheduler do its thing... beautiful!
 
Right, noted 🙂

So... a program like this guys CPU stress program:

http://weblogs.asp.net/kennykerr/ar...for-your-developer-toolbox_2E002E002E00_.aspx

It would work by having a new thread for what you set it to? Wait never mind, that's pretty much what it says in the description 😳

And this is how you find the number of cores!

http://stackoverflow.com/questions/4759570/finding-number-of-cores-in-java

Its all coming together now :biggrin: Use that method and then spawn a number of threads based on how many cores there are and let the OS's scheduler do its thing... beautiful!

Unless you absolutely need the fine grained control over thread lifecycles, I would recommend that you stay away from spawning new threads and instead use a thread pool. Java has some pretty good built in threadpools and Guava has everything else that you would ever want when it comes to concurrency.

Spawning and killing threads can be tricky, so why do it if you don't have to?
 
Unless you absolutely need the fine grained control over thread lifecycles, I would recommend that you stay away from spawning new threads and instead use a thread pool. Java has some pretty good built in threadpools and Guava has everything else that you would ever want when it comes to concurrency.

Spawning and killing threads can be tricky, so why do it if you don't have to?

Not sure what a thread pool is or how to use one 😕 Ill have to google that. Up until now ive just been creating new threads like objects and then using .start() to run em, nothing too complex.
 
A thread pool is a library which manages a lot of the complexities of working with a collection of threads for you.

Dave
 
Not sure what a thread pool is or how to use one 😕 Ill have to google that. Up until now ive just been creating new threads like objects and then using .start() to run em, nothing too complex.

As Apathetic says, it is a way of managing the complexity of lots of threads. It automatically kills, spawns, and maintains (puts to sleep) the threads it is maintaining. You, the user, simply say to the thread pool "Complete this task for me" and it does it. In java, this is done via giving the thread pool either a runnable (returns nothing) or a callable (returns something). The thread pool returns a future (a wrapper around the object returned which blocks on the completion of the task when you call the "get" method).

The beauty of thread pools is that often, when dealing with concurrency, you really don't care about the process that is running, you just have a bunch of tasks you want to do at the same time. Thread pools make this a lot easier to maintain.
 
Back
Top