Question

If number of threads are increased from nThread to nThread + 1, the speed decreases by half.

ExecutorService executor = Executors.newFixedThreadPool(nThread);

If I just set nThread to 1, it doesn't use all my cores. What's going on?

My task doesn't involve reading file or network. It creates objects and computes. However, it reads a data from a vector.

Can multiple threads reading data from a same vector decrease performance? If so, how can I fix then?

Was it helpful?

Solution

A vector is an old list implementation that relies on a lock to provide threadsafety. If multiple threads at the same time are accessing that vector, these threads will suffer from lock contention and that is probably what you are experiencing now.

If the vector is only read from, I would replace it by an ArrayList (or an array). Because no locking is done, and in case of a readonly data-structure, isn't needed.

OTHER TIPS

If number of threads are more then irrespective of number of tasks, context switching will be slow as threads in a thread pool executor are of same priority and CPU has to be shared amongst them. Also more are number of threads , more are the chances of threads waiting for a monitor.

Even if there is no synchronization, more number of threads can heavily affect performance. In one of the applications I have worked upon, there was a task of xml parsing which took 100 ms, increased to 5 seconds when number of threads increased from 10 to 50.

Configuring thread pool is a learn and implements thing. It do depends on the no of cores in CPU, More cores will allow more parallel processing.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top