1

I'm creating the following threadPoolExcutor:

int n = Runtime.getRuntime().availableProcessors();
ExecutorService executor = Executors.newFixedThreadPool(n);
for( int i = 0; i < n; i++ )
    executor.execute(new Work());

Now, I have 8-cores system. Is it reliable by JVM that all 8-cores will be usede for executing the work?

4
  • 1
    Is it necessary to use concurrency? Have a look at martinfowler.com/articles/lmax.html Commented Jul 29, 2015 at 9:12
  • @BertramNudelbach In my particular case the synchronization is very simple. So why don't we use concurrency? Commented Jul 29, 2015 at 9:14
  • 1
    Just because "writing concurrent code is very hard" :) But I get the point that you're not questioning the decision to use concurrency but rather how the JVM/system implements it. It's just that I always remember LMAX when hearing about concurrency and performance. Commented Jul 29, 2015 at 9:17
  • this depends on a number of factors, what Work exactly does (for instance some operations won't trigger the hyperthreading scheduler, if you have some IO operations more threads might be better), on which operating system you run it (how many threads are actually used is decided by the JVM and the OS scheduler) and probably several other factors. If I were you I would simply benchmark it a little bit. Commented Jul 29, 2015 at 9:23

1 Answer 1

2

The API says about the availableProcessors() method:

Returns the number of processors available to the Java virtual machine.

This value may change during a particular invocation of the virtual machine. Applications that are sensitive to the number of available processors should therefore occasionally poll this property and adjust their resource usage appropriately.

So it is not guaranteed to be the 8 cores that your system has but rather the number of processors that your JVM can use.

Whether all cores are used also depends on the amount of work that you put in. When you only execute one item of "Work" it will only occupy one thread - sometime in the future. If you need the result I would recommend using a Future.

Sign up to request clarification or add additional context in comments.

2 Comments

I don't need a result. So, all available to JVM processors will be used if we have quite large amount of works to be exectued?
In theory you can have way more threads than you have processors on your system - the jvm would take care of the thread scheduling for you then. So, given that you only have one main thread (the one invoking the code snippet that you have above) and then your 8 threads - you would already have 9 potential threads active at the same time. If all of them take long enough to be completed they should use all 8 cores (but one of them would be sleeping at any time).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.