Showing posts with label parallelism. Show all posts
Showing posts with label parallelism. Show all posts

Monday, May 11, 2009

CyclicBarrier

A new section of the Javamex web site looks at the CyclicBarrier class. In case you haven't come across it, CyclicBarrier is a construct that makes coordinating threads easier. Unlike CountDownLatch, which is designed for "one-off" coordinated actions, CyclicBarrier is designed to be re-used, so that it is suitable for iterative processes where the participating threads need to repeatedly perform a parallel operation, but periodically "converge" so that their results can be amalgamated.

In the article, we discuss the example of using a CyclicBarrier to coordinate a parallel sort algorithm. Essentially, the sort takes place in three stages. Each of the stages occurs in parallel, but at the end of each stage, a small single-threaded step must take place to amalgamate the results of the previous parallel operation.

Essentially, the code for a worker thread involved in the operation repeatedly carries out an operation and then calls the barrier's await() method. The latter method blocks until all participating threads have also called await() (we sometimes call this "arriving at the barrier"). At that point, CyclicBarrier executes our pre-determined "amalgamation" routine on one of the threads (the last one to arrive at the barrier, in fact), and then relases all the threads to move on to the next stage. Overall, the class takes out a lot of the actual thread coordination work, although, as we discuss in the article, we must still think about regular concurrency issues such as data synchronization and lock contention.

An additional feature of CyclicBarrier which we discuss is that it handles propagation of interruptions to all participating threads. In other words, if any thread involved in the operation is interrupted, then the whole operation will cease once all threads have called the await() method.

Whilst definitely one of the lesser used of the Java 5 concurrency utilities, CyclicBarrier is definitely a useful class that should not be overlooked.

Friday, December 5, 2008

New section: using threads in Java

The first articles in this new section look primarily at how to use "raw" threads in Java. As well as the basics such as how to create a thread and stop or interrupt it, the section looks at more advanced threading topics such as:

How threads work

A look at threads "under the hood": an examination of some of the details of thread scheduling, and the implications of different scheduling algorithms for Java threads and the methods that control them.

Thread priorities

How they work (or rather, how they don't work...) on specific operating systems. Did you know, for example:
  • on Linux, setPriority() has no effect pre Java 6, and that even then you need to run as rootand use a special command-line flag?
  • on Windows, the implementation of setPriority() changed between Sun's Java 5 and Java 6 implementations?
  • on Windows, thread priorities have little effect on threads competing for CPU?
See the article for more information.

sleep() and yield()

Information on the limitations and behaviour of sleep() under different load, sleep granularity, and bugs in the Windows implementation. And finally, find out what yield() actually does...!

Suggestions welcome!


As with all sections of the Javamex site, suggestions are very welcome here for new topics or specific questions you'd like to see answered. Updates and additions will be added periodically. Corrections are also very welcome since, particularly in the case of the some of the threading topics, I have tried to pull together information which is elusive or described in contradictory ways in different sources!