Fine tune Java programs

13 Nov 2015

There is simply no substitute for the experience of writing and tuning your own parallel programs.

Well, as you will very soon find out, the stuffs presented here are a little unorganized. My apology! For one thing, I’ve been dumping things that I thought useful over the time here. Hopefully in the future I will find some time to sort them out. For another, I feel the experience/knowledge I gained related to the contents presented here has come into the spectrum of “tacit knowledge”, which is well known for being hard to explain (imagine teaching people how to swim or ride a bike). However, I did come across a pretty organized blog on the same matter, in case you are interested.

Multi-thread and parallele programming


Since CoJava applies Java fork-join parallelism framework, this post is mostly concerned with efficiently parallelizing fork-join computations in Java. The fork/join framework uses a thread pool in which a fixed number of threads are created. Each thread has a queue of tasks that are awaiting a chance to execute. When a task is started (forked), it is added to the queue of the thread that is executing its parent task. Because each thread can be executing only one task at a time, each thread’s task queues can accumulate tasks which are not currently executing. Threads that have no tasks allocated to them will attempt to steal a task from a thread whose queue has at least one task - this is called work stealing. By this mechanism, tasks are distributed to all of the threads in the thread pool. By using a thread pool with work stealing, a fork/join framework can allow a relatively fine-grained division of the problem, but only create the minimum number of threads needed to fully exploit the available CPU cores. Typically, the thread pool will have one thread per available CPU core. There are some rules of thumb regarding general parallel programming (credit goes to the course website of CMU 15-418/618)