![]() |
![]() |
|
02158 Concurrent Programming Fall 2024 |
Lab 1: Dynamic Java Thread Creation/Cancellation |
Home | Plan | Material |
Furthermore, the lab will illustrate some effects of multiprocessing.
Assistance for this programming lab will be available in Building 303A, area EAST, Thursday September 12, 15.00-17.00
You should consult [Proc 4] and perhaps [Andrews 5.4.1] to see how to create and terminate Java threads.
The lab is easily run from the command line, but you may also
import the files into a project of your favourite IDE.
Explain this. How many threads are active in this program?
Also, when terminating the program using command 'x', the
termination of this thread must be awaited before closing the window
(perhaps issuing status messages).
To determine whether a started thread t is still active, t.isAlive() should be used. To await that it becomes terminated, t.join() should be used.
Notice that a thread which 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 active threads.
Modify the Task to consume CPU-cycles by setting set
the useCPU flag in run().
You may have to adjust the delay-factor of
the inner loop in order to obtain a suitable speed (say 5-10 seconds
to run a single task). The speed variation is automatically set to
0 to get comparable observations.
Before starting more than one task at a time, try to predict the effect on the execution.
Now try to see what happens when you start more than one tasks (almost) at the same time. Do you observe any speed reduction and for which numbers of tasks? Explain your observations.
[If your computer has 4 or more cores, you may have to increase the number of text fields N in the source code to say 16 or 20 to see a speed reduction.]
Explanation: Since sleep is no longer called regularly, the
InterruptedException will not be thrown.
Instead, a CPU-intensive task will have to use the boolean function
interrupted() to test (and clear) the value of the interrupt flag
where appropriate. Cancel the thread by this means.
Meanwhile, you could try out Mini Lab 2.
Enjoy!
In most GUI-frameworks, such as the Swing-library used here, the window components are implemented all in Java, and for efficiency reasons not thread-safe. That means that only the event-dispatcher thread (aka the GUI thread) should call Swing components. By calling appropriate operations, other threads may post their updates within the event queue for subsequent processing by the event-dispatcher thread.
In the given program, the command handling is executed in a separate thread (actually the main thread) and key presses are sent via a buffer from the GUI thread to the command thread. To print on the display, the command thread uses the println() method which posts the printing to the event queue.
Also the Task-class has a method setMyText() which test whether the caller is the GUI thread or not. In the latter case, the method EventQueue.invokeLater() is used to post the text update on the event queue.
The Swing library provides the special class SwingWorker which facilitates asynchronous background execution. For details of using this, see the article:
http://docs.oracle.com/javase/tutorial/uiswing/concurrency
The same considerations apply to almost all GUI-frameworks, eg. .NET (WF, WPF), Eclipse SWT, JavaFX and Android which all provide specialized frameworks for doing asynchronous background processing.
Hans Henrik Løvengreen, Sep 12, 2024 |