Java Multitasking involves multiple Processes
Multitasking derives the mechanism to run many processes simultaneously with user interaction.
You can try this student exercise to illustrate multithreading based on the white board applet you are already working on.
There are many examples of where you want to use or need to use multithreading.
Multitasking Examples
- a simulation of traffic flow in your city,
- a model of the stock market,
- an arcade-style video game,
- a word processor that identifies misspelled words in the background as you type,
- a program that downloads images over the Web as you perform other work,
- a spreadsheet of live data collected over the network that keeps track of the status of a business.
All of these require multitasking to run correctly.
Preemptive multitasking[1] is something that Java can deliver, and having an understanding of multitasking is crucial to writing programs in Java.
These exercises can help demonstrate the functionality of multitasking in Java.
Java Language Reference
Threading Requirement
Many popular languages support threading, either inherently or through the use of external libraries. In spite of this, it is possible that you might not have used threads at all. Most of the time, when you write a program, creating threads is not a general requirement. Whatâs more, writing multithreaded code is hard and debugging such code makes it worse.
So do we really need to care about learning threads? Yes, we do. The programs you studied so far have been trivial, focusing on a certain Java language feature. In real-life applications, it is hard to find a Java program that does not use threading.
In fact, Java developers considered threading to be so important that the threading libraries were introduced from the beginning in JDK 1.0. back in 1996
Multithreading
Imagine a stockbroker application with a lot of complex capabilities.
- One of its functions is to download the latest stock option prices,
- check prices for stop-loss, and
- analyze historical data for the company IBM.
In a
single-threaded runtime environment, these actions execute consecutively, meaning the next action can happen only when the previous one is finished. If historical analysis takes half an hour,
and the user selects to perform a download and check afterward, the warning may come too late to liquidate the stock as a result.
We just imagined the sort of application that requires
multithreading[2].
Ideally, the download should happen in the background using another thread.
Using this technology, other processes could happen at the same time so that, a warning could be communicated instantly. Meanwhile, the user is interacting with other parts of the application. The analysis, could also happen in a separate thread so the user can work in the rest of the application while the results are being calculated
[1]
Preemptive multitasking: Preemptive multitasking is a task in which a computer operating system uses some criteria to decide how much time to allocate to any one task before giving another task a turn in using the operating system.
[2] Multithreading: Multithreading is a mechanism that runs various threads under a single process within its own space.