20-CS-694 Advanced Programming Techniques Spring 2012
Threads

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

All lectures
Stopping, Deadlock etc.

Instructions for applets:

All applets have a JButton that says 'Press Me'. Hit that button.

    1.   Stop1.java

    Stop a thread by setting a flag in the run method. As long as the flag is true a loop continues to run. When the flag is set to false the loop is exited then the run is exited and the thread dies.
 
    2.   Stop2.java

    Stop a thread with interrupt. If a security manager is present, the flag mentioned above can be set from within the checkAccess method. The checkAccess method is invoked when interrupt()() is invoked from the thread object.
 
    3.   Stop3.java

    Stop a thread with interrupted(). The interrupted() method can be used instead of the flag above. Its value becomes true after interrupt() is invoked from the thread object.
 
    4.   Stop4.java

    Stop a thread by invoking checkAccess(). In case a security manager is set, checkAccess() can be invoked to set the flag in run's loop.
 
    5.   DeadLock.java

    Deadlock example. This is an attempt to implement a simple handshake. There are two threads and two monitors: one thread is associated with each monitor. Either thread invokes a ping method from its monitor. Inside the ping a confirm method of the other monitor is invoked. When a reply is obtained from both parties, the handshake is complete. Unfortunately this implementation sometimes gets stuck: both threads may be in their pings waiting for confirms that cannot happen.
 
    6.   DeadLockFixed.java

    A fix for the above under certain conditions. A wait and two notifys are added.