Suspending a thread is when we temporarily pause a thread, rather than stop and destroy it. This thread can be resumed at a later stage. When Java was first introduced threads were a part of the language and worked fine. There were some difficulties with multi-Platform versions. With the introduction of Java 2 threads were updated to prevent certain race and lock conditions that occurred. In the Figure 8.3, “A Multi-Threaded Counter” displays a capture of an application running in which there are two TextFields object that are controlled by the same buttons, but they are different threads that start at different points Figure 8.3 A Multi-Threaded Counter (a) shows the count values at the start, (b) shows the count values after a few seconds, where the first counter has passed the second counter. The source code is as below:
I have changed the code from the previous section to have two separate classes - the application and the . I did this to prevent any confusion between the
The
The
Once one of these things happens the thread then becomes available to the scheduler. The Task: Modify the code as shown in Figure 8.3, “A Multi-Threaded Counter Example” to add the following functionality:
You can see a screen-grab of my solution in Figure 8.4, “My Up/Down Counter Example”. Figure 8.4 The Up/Down Counter Example, for you to try yourself. Solution: The solution is here - UpDownCounterApp.java but please do not look at it until you have had a good attempt yourself.Java has thread scheduling that monitors all running threads in all programs and decides which thread should be running. There are two main type of thread:
There are two main forms of scheduler: (i) Preemptive that gives a certain time slice to each thread. The scheduler sets up the order that the threads run in. (ii) Non-preemptive that runs a thread until it is complete. Each thread has control of the processor for as long as it requires. New threads inherit the priority and daemon flag from the thread that created it. The scheduler decides which thread should be running based on a priority value assigned to the thread. The priority number has a value between 1 and 10 and a thread runs more often if it has a higher priority value. There are three pre-defined priorities:
You can use the I have written a short example based on the code in the previous section to show you the effect of priorities on threads. This example sets the first Figure 8.5 The Counter Example, with different thread priorities (a) you can see that count 1 has a lower value but at a later time in (b) you can see that counter 1 has a higher value than counter 2. ![]() The code for this example is below, with the changes highlighted in yellow (please note, I am using the same Counter class, I have had to rename it in order that I can distributed the notes in a single ee402 package):
These notes are copyright Dr. Derek Molloy, School of Electronic Engineering, Dublin City University, Ireland 2013-present. Please contact him directly before reproducing any of the content in any way. |