user3663882
user3663882

Reputation: 7357

Using concurrency for improving performance

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?

Upvotes: 1

Views: 102

Answers (1)

nutfox
nutfox

Reputation: 484

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.

Upvotes: 2

Related Questions