We often require to break our application into a number of independent sub-tasks, called threads (threads of execution). We write each thread in an individual manner and assume that there is some mechanism for dividing up the CPU time and sharing it between these threads. Threads enhance performance and functionality, by allowing a program to perform multiple tasks simultaneously. Why would we want to do this? Well, take the example of a web browser. It allows us to download a web page and at the same time interact with the window, and even scroll through the text reading what has already started downloading. Threading is supported directly in the Java language, allowing us to write platform independent threaded applications (There are some difficulties when dealing with the different operating systems as there are slight behavior differences). Multiprocessing refers to multiple applications, executing 'apparently' concurrently, whereas multithreading refers to one or more tasks within an application executing 'apparently' concurrently. The word 'apparently' is used as the platform usually has a single CPU that must be shared. If there are multiple CPUs it is possible that processes might actually be executing concurrently. This is the case with more recent Intel processors, such as the i3, i5 and i7 where there are multiple cores. Multiprocessing is a heavyweight process, under the control of the operating system. The different applications have no relationships with each other. Multithreading can produce programs that are very efficient, since work can be performed during low CPU actions, such as waiting for the user to enter data, or waiting for a file to load. Multithreaded programs can be concurrently executing in a multiprocessing system. They can exist concurrently with each other. Suppose we wish to write an application that performs a CPU-intensive operation. It is possible that this application will become unresponsive as the application ignores user input. For example, this counter application simply counts forever, outputting the count in a Textfield component. Figure 8.1, “The Bad Counter Application Running” displays a capture of an application that does not work correctly. You can press the Start button to start the count but the Stop button will not work to stop the application from counting. Note: This application does not work correctly - It may even have to be killed by using your task manager. Under Windows press CTRL-SHIFT-ESC together to open the task manager. Figure 8.1 The Bad Counter Application Running Here is the source code for this example. If you have not studied threads before then it will not be immediately obvious why this application does not work correctly.
So why does it not work? When you run the application you will see that it starts counting as expected with a delay between each number - so what is wrong? Well if we look at the code step by step:
We want to re-write the application in the previous section so that we can do this count operation, while still providing access to the user interface. This is not much different than most threaded applications, where we might define several threads:
The computation thread could be given priority once the load or input threads are blocked, waiting for communication from the keyboard or the disk drive. Sequential programs do not have this facility. They cannot perform any operations while the user is deciding what to type. We have two ways of creating threads in Java. We can extend the The first way is to extend the 1 2 3 public class MyThread extends Thread 4 { 5 public void run() 6 { 7 //add your implementation here 8 } 9 } 10 11 Implementing the 1 2 public class MyThread extends WhateverClass implements Runnable 3 { 4 public void run() 5 { 6 //add your implementation here 7 } 8 } 9 10 This section fixes the counter applet discussed in the section called “Why use threads?” to allow it to work correctly. Figure 8.2, “The Bad Counter Fixed Application Running” displays a capture of a working version of the application. You will notice that when you press the "Start" button the counter starts, but you are still able to press the "Stop" button and it does indeed stop the counter. I have not written the code to allow you to restart the counter - for simplicity at this point. Figure 8.2 The Bad Counter Fixed Application Running The source code is below, with the main changes highlighted in yellow:
So why does this version work? Well I have made a few changes:
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. |