The Java Tutorial on threads


Thread, Runnable, synchronizednotify


The three rules from Doug Lea :
  1. Always lock during updates to object fields
  2. Always lock during access of possibly updated object fields
  3. Never lock when invoking methods on other objects


Four examples of Ping-Pong  : et (Thread vs. Runnable), et (using synchronized).

Producer / Consumer

An example of producer/consumer between several threads with a shared buffer :,,
A fairly similar example with an infinite loop and different velocities :,,


This example draws two curves at the same time using two threads. It is composed of two files : and Please note the use of a handle in the class Curve : the abstract method function(double x), and java2D for drawing curves.
A second version, more sophisticated,, uses a shared memory: between a reading thread and a writing thread


A simple example about the use of threads, composed of two classes : and The method increase() in Counter illustrates Lea's three rules :


This example demonstrates Doug Lea's three rules. It is composed of three files :, and
In a second version, the moves of the particles are simply interrupted and not initialized again as in the first version. Only has been modified.