Examples of Programs that use Threads

To view these programs you will need a browser that supports Java 1.2, Java 1.3, or Java 2. That is, Netscape 4.x with appropriate plugins, or Java-enabled versions of Netscape 6 and Microsoft IE 5.


MAIN APPROACHES TO USING JAVA THREADS

PROGRAM AND SOURCE CODE

1. Making a Thread Subclass. The first mechanism for using Java threads is making a thread subclass. This program creates threads that randomly print output to the screen by making a thread subclass (Counter.java). Notice that all the actions (i.e. printing to the screen) take place in the run method of the subclass (Counter.java). The driver class (CounterTest.java) creates instances of a Thread subclass (Counter.java) and calls the instances' start method to start each thread. Adapted from the Core Web Programming by Hall M., 1997

Output
Source code : Counter.java
CounterTest.java
2. Implementing Runnable. The second mechanism for using threads is implementing the runnable interface. This program creates threads that randomly print output to the screen by implementing the Runnable interface. The main class (Counter2.java) executes both the start and the run methods with this approach. Adapted from the Core Web Programming by Hall M., 1997

Output
Source code : Counter2.java
Counter2Test.java

Another Example of Implementing Runnable Interface. In this example the Clock class must use the second approach to using threads, using Runnable, because the Clock class is a subclass of the Applet class. This holds true for any class that must inherit another class. This applet updates the clock time every second. Adapted from The Java Tutorial by Campione M. et all, 2000.


Run the program
Source code : Clock.java
SYNCHRONIZATION

Producer/Consumer Example (Synchronized). In this example the Consumer accesses and prints each value the Producer stores in the CubbyHole object exactly once. The Consumer should not access the CubbyHole when the Producer is changing the CubbyHole's value. Likewise the Producer should not access the CubbyHole when the Consumer is getting the value. Therefore, the put and get methods in the CubbyHole class contain the synchronized keyword. Also, in order to coordinate the Producer and Consumer threads wait() and notifyAll() are used in both the put and get methods of the CubbyHole class. Adapted from The Java Tutorial by Campione M. et all, 2000


Output
Source code : Producer.java
Consumer.java
CubbyHole.java
ProducerConsumerTest.java
Producer/Consumer Example (Unsynchronized). In this unsynchronized version the Consumer acesses the same value more than once and also misses values that are stored by the Producer. Only the CubbyHole class is different in the unsynchronized version. For this example, you can use Producer.java, Consumer.java and ProducerConsumerTest.java from the synchronized version above. Adapted from The Java Tutorial by Campione M. et all, 2000

Output
Source code : CubbyHole.java
ANIMATION

Bouncing Circle. This applet contains an animation of a circle bouncing around on the screen. This animation has a flicker problem due to the updating of the display. The next example shows how Swing can solve the flicker problem.

Run the program
Source code : BouncingCircle.java
BouncePanel.java
MovingCircle.java
SimpleCircle.java

Bouncing Circle (Double Buffering). Just like in the previous example, this applet contains an animation of a circle bouncing around on the screen. However, in this example double buffering is used to get rid of the flickering problem. Double buffering is a technique that draws into an offscreen buffer and then copies the completed work to the display in a single painting operation. Although you can implement the double buffering technique yourself, as shown in a later example, Swing supplies double buffering for free. All you need to do is use a Swing component in a Swing container. Swing takes care of the details. Notice that a JPanel Swing component replaces the AWT Panel in BouncingCircle.java. This JPanel component implements double buffering and gets rid of the flickering problem. SimpleCircle.java, MovingCircle.java and BouncePanel.java remain the same as in the previous example while only one line of code is changed in BouncingCircle.java


Run the program
Source code : BouncingCircle.java
Bouncing Circles. This applet contains circles bouncing around on the screen. Does not use double buffering so has problems with overlapping circles. Overrides update to avoid flicker problems. Run the program
Source code : BouncingCircles.java
BouncePanel.java
MovingCircle.java
SimpleCircle.java
Bouncing Circles (Double Buffering-Offscreen Drawing). Just like in the previous example this applet contains circles bouncing around on the screen. However, this time double buffering is used for speed and to avoid problems with overlapping circles. All circles are drawn onto an offscreen image and then copied to the display area. BouncingCircles.java, SimpleCircle.java and MovingCircle.java remain the same as in the previous example. The double buffering is implemented in BouncePanel.java

Run the program
Source code : BouncePanel.java


ACKNOWLEDGEMENTS

  1. Campione M., Walrath K., Huml A. (2000), The Java Tutoral, Third Edition , Addison-Wesley
  2. Hall M. (1997), Core Web Programming (Core Series), Prentice Hall Computer Books


Developed in March 2001 by Vasilios Lagakos and Mark Austin
Copyright © 2001, Department of Civil Engineering, University of Maryland