Introduction to Thread Lightweight Heavyweight Java
If only one process exists before the processor and if the process execution is stopped for some reason, the microprocessor becomes idle without any work. This type of programming is known as monolithic programming. In monolithic programming, the whole process constitutes a single module. To get a better understanding, Lord Buddha statue in Hussain Sagar lake in Hyderabad, India is a monolithic statue (single stone carvation). The minus point of single stone is if some part is to be replaced in long future, the whole statue is to be replaced. The same is the problem with monolithic programming also; if a part of the program is stopped, the whole program comes to standstill. There comes to the rescue multithreading.
A Java process can be divided into a number of threads (or to say, modules) and each thread is given the responsibility of executing a block of statements. That is, a thread is an incharge of a few statements of a program. If one thread stops its execution, the execution control can be shifted to another thread. Remember, here all threads belong to the same process. This is the advantage of threads over monolithic programming. The ultimate result is microprocessor idle time is reduced. The end result is output of the program comes earlier.
A thread is a part of a process. A thread lives within a process. No process, no thread. To understand better, we cannot keep our brain outside for sometime and place it back; it is because brain is a part of human body. Similarly, a thread cannot be there without a process. Infact, a process is divided into a number of threads.
Now let us define a thread; "a thread is a single sequential flow of control within a process". It is something confusing. Let us be clear. As said earlier, a thread comprises of a block statements. These statements are executed one-by-one sequentially. Every thread maintains its own execution context within a process context area. We can say, a thread is a sub process. For execution, one thread does not depend on other; every thread is independent of other. This tutorial gives you how to divide a process into a number of threads or to say, how to create threads, how to assign different tasks, priorities and synchronization
Lightweight Process and Heavyweight Process
If the execution shifts between the threads of the same process, it is known as lightweight process. If the execution shifts between the threads of different processes, it is known as heavyweight process.
In the above figure, if the control shifts between the threads t1 and t2 of the same process p1, it is known as lightweight process. If the control shifts between the threads t1 of process p1 and t1 of process p2, it is known as heavyweight process. Generally, threads are designed to be lightweight because heavyweight is a big overhead to the OS.
Advantages of Multithreading
First and foremost advantage is, as you have understood so far, microprocessor idle time is reduced and consequently output is obtained faster. When a program is divided into number of threads, each comprising a few statements, the code becomes simpler, readability increases, debugging and maintenance becomes easier. Generally, user's input is very slow in GUI environment. This late user's response is best utilized by other threads (reduces overall execution time).
Applications of Multithreading/Multiprocessing
- Playing media player while doing some activity on the system like typing a document.
- Transferring data to the printer while typing some file.
- Downloading some files like images while browsing is going on (downloading images is very slow).
- Running animation while system is busy on some other work.
- Honoring requests from multiple clients by an application/web server.
- Threads are useful in handling events generated by components like mouse etc. A thread is capable to produce a frame with images.
Java API support for Threads
Multithreading is one of the built-in and important feature of Java language. Java's built-in classes, useful to develop a multithreaded program, are hereunder and all are from java.lang package.
|Thread||sleep(), join(), suspend(), resume(), stop() and run() etc.|
|Runnable interface||only run() method|
|Object||wait(), notify() and notifyAll()|
|ThreadGroup||getName(), getParent() and activeCount() etc.|
Common Exceptions in Threads
The following are common exceptions that come in thread programming. All are from java.lang package.
- InterruptedException : It is thrown when the waiting or sleeping state is disrupted by another thread.
- IllegalStateException : It is thrown when a thread is tried to start that is already started.