20-CS-694 Advanced Programming Techniques Spring 2012
Threads

Interfaces, Exceptions, Graphics, Animation, Threads, Reflection, Networking, RMI, JDBC, JNI

All lectures
Introduction, Producer-Consumer

Instructions for all applets:
All applets contain a 'Press Me' button. Click it. Some applets also have a choice of modes. All applets come in pairs. The difference between two applets of a pair is that the control portion of the code in the upper applet is Threaded (class Test?Doer) and this portion of code is not threaded in the lower applet. Sometimes this affects whether output can be seen.

Classes common to all files below:
AuxFrame.java and TestFrame.java

    1.   Test1b.java


Test1a.java


  -   notify, notifyAll: A simple test of notify, notifyAll, wait, synchronize. The Monitor M1 allows W1 objects to wait (via method 'a') until awakened by an N1 object (via method 'b'). The user may choose to wake up threads using notify or notifyAll.
 
    2.   Test2b.java


Test2a.java


  -   notify, notifyAll: This does the same as the above except now there are two M1 monitors and two W1 threads wait in one monitor and another W1 thread waits in the other monitor. A single N1 thread invokes the b method of just one of the monitors.
 
    3.   Test3b.java


Test3a.java


  -   notify, notifyAll: Similar to the applet above except the main thread of Test3Doer in the 1st case and Test3aFrame in the case of the 2nd applet yields right after starting all the W2 threads.
 
    4.   Test4b.java


Test4a.java


  -   Cause N3 objects to wait for all W3 objects to finish. The W3 objects are awakened one at a time. A hashtable stores all invoking W3 threads. When each finishes it is removed from the hastable and wakes up the next thread. When the hashtable is empty the N3 threads are released.
 
    5.   Test5b.java


Test5a.java


  -   sleep has the same effect as yield. Same as two applets up except that sleep is used to suspend the main thread instead of yield.
 
    6.   Test6b.java


Test6a.java


  -   producer-consumer. A Producer object puts tokens into a "stream" that is managed by monitor Monitor and a Consumer object takes them out of the stream. No new tokens enter the stream unless all tokens have been pulled out.
 
    7.   Test7b.java


Test7a.java


  -   producer-consumer. This is a refinement on the above. The monitor becomes an abstract Stream class. A Producer subclasses this class.
 
    8.   Test8b.java


Test8a.java


  -   interrupt. A test of interrupt to wake up a thread.
 
    9.   Test9b.java


Test9a.java


  -   Threads can be tricky. Example showing how tricky Threads can be. Printing is directed to standard out as well as a JTextArea but in the 2nd applet (Test9a.java) nothing appears in the JTextArea. In the 1st applet (Test9b.java) printing to the JTextArea works great. The difference is the control portion of the code is Threaded in this applet (Test9Doer) but not in the sister applet.