![]() |
![]() |
|
02152 Concurrent Systems Fall 2008 |
CP Lab 1: Dynamic Java Thread Creation/Cancellation |
Home | Plan | Material |
Assistance for this programming lab will be available in the E-databar, Building 341, rooms 003+015, Thursday, September 4, 15.00-17.00.
You should consult [Proc 4] and perhaps [Andrews 5.4.1] to see how to create Java threads.
Notice that a thread that has terminated cannot be restarted. Instead a new thread object must be created.
Hint: Use an array of Thread (or Task) to keep track of the threads.
Since sleep is no longer called, the
InterruptedException will not be
thrown. Instead, the task may use the boolean function
interrupted() to test (and clear) the value of the interrupt flag
where appropriate. Cancel the thread by this means.
Enjoy!
This purpose of this lab is to illustrate the possibility of off-loading GUI-activity to threads. Using the older AWT-GUI framework, this can be done with only minor modifications of the of the code. The reason is that the AWT-framework uses the underlying, native window components which are (assumed to be) thread-safe. This implies that concurrent threads may call update functions in the window components without risking them to be rendered inconsistently or breaking down (eg the window "freezing").
However, in the newer Swing-library, the window components are implemented all in Java, and for efficiency reasons the Swing components are not thread-safe. That means that only the event-dispatcher thread should call Swing components. By calling appropriate operations other threads may register their updates within the event queue for subsequent processing by the event-dispatcher thread.
For details of using Swing from separate threads, see the articles:
http://java.sun.com/products/jfc/tsc/articles/threads/threads1.html
http://java.sun.com/products/jfc/tsc/articles/threads/threads2.html
http://java.sun.com/products/jfc/tsc/articles/threads/threads3.html
PS. The same considerations apply to Windows Forms under .NET. See eg:
http://msdn2.microsoft.com/en-us/library/ms171728.aspx
Hans Henrik Løvengreen, Sep 1, 2008 |