Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parallelization: What causes Java threads to block other than synchronization & I/O?

Short version is in the title.

Long version: I am working on a program for scientific optimization using Java. The workload of the program can be divided into parallel and serial phases -- parallel phases meaning that highly parallelizable work is being performed. To speed up the program (it runs for hours/days) I create a number of threads equal to the number of CPU cores on the machine I'm using -- typically 4 or 8 -- and divide the work between them. I then start these threads and join() them before proceeding to a serial phase.

So far so good. What's bothering me is that the CPU utilization and speedup of the parallel phases is nowhere near the "theoretical maximum" -- e.g. if I have 4 cores, I expect to see somewhere between 350-400% "utilization" (as reported by top) but instead it bounces around between 180 and about 310. Using only a single thread, I get 100% CPU utilization.

The only reasons I know of for threads not to run at full speed are: -blocking due to I/O -blocking due to synchronization

No I/O whatsoever is going on in my parallel threads, nor any synchronization -- the only data structures shared by the threads are read-only, and are either basic types or (non-concurrent) collections. So I'm looking for other explanations. One possibility would be that several threads are repeatedly blocking for garbage collection, but that would only seem to make sense in a situation with memory pressure, and I am allocating well above the required maximum heap space.

Any suggestions would be appreciated.

Update: Just in case anyone is curious, after some more investigation I tweaked the code for general performance and am seeing better utilization, even though nothing I changed has to do with synchronization. However, some of the changes should have resulted in fewer new heap allocations in particular I got rid of some use of iterators and termporary boxed numbers (The CERN "Colt" library for high-performance Java computing was useful here: it provides collections like IntArrayList, DoubleArrayList etc for basic types.). So I think garbage collection was probably the culprit.

like image 227
Joe Avatar asked Dec 02 '08 07:12

Joe


People also ask

What is the alternative to synchronized in multithreading?

concurrent package has a whole range of classes that can be used to make multi-threaded code thread-safe without explicitly using the "synchronized" keyword, if that's what you're asking. The ThreadLocal class may also help, as may the "volatile" keyword.

What will happen if two threads try to modify same resource without synchronization in Java?

A: When more than one thread try to access same resource without synchronization causes race condition. So we can solve race condition by using either synchronized block or synchronized method.

How do ensure threads don't override each other?

To avoid deadlock, you should always make sure that multiple threads acquire the same synchronization locks in the same order. If both threads synchronized on Boolean. class and then Deadlocker. class (or the other way around), we would have avoided the risk of deadlock.


1 Answers

All graphics operations run on a single thread in swing. If they are rendering to the screen they will effectively be contending for access to this thread.

If you are running on Windows, all graphics operations run on a single thread no matter what. Other operating systems have similar limitations.

It's actually fairly difficult to get the proper granularity of threaded workers sometimes, and sometimes it's easy to make them too big or too small, which will typically give you less than 100% usage of all cores.

If you're not rendering much gui, the most likely culprit is that you're contending more than you think for some shared resource. This is easily seen with profiler tools like jprofiler. Some VM's like bea's jrockit can even tell you this straight out of the box.

This is one of those places where you dont want to act on guesswork. Get a profiler!

like image 100
krosenvold Avatar answered Nov 02 '22 23:11

krosenvold