Thread Lightweight Heavyweight Java

Thread Lightweight Heavyweight Java

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.


Thread Basics Lightweight Heavyweight

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.

Thread Basics Lightweight Heavyweight

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

  1. Playing media player while doing some activity on the system like typing a document.
  2. Transferring data to the printer while typing some file.
  3. Downloading some files like images while browsing is going on (downloading images is very slow).
  4. Running animation while system is busy on some other work.
  5. Honoring requests from multiple clients by an application/web server.
  6. 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.

Class Useful Methods
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.

  1. InterruptedException : It is thrown when the waiting or sleeping state is disrupted by another thread.
  2. IllegalStateException : It is thrown when a thread is tried to start that is already started.

13 thoughts on “Thread Lightweight Heavyweight Java

  1. suraj bahadur

    hello sir,
    would you please define JAVA API METHODS AND CLASS IN DETAILS WITH EXAMPLE .IT IS NOT EASIER TO UNDERSTAND .
    plz suggest any book which is fully on deep concept in java with program in details

    1. S. Nageswara Rao, Corporate Trainer

      All the packages and classes that comes with JDK installation are known as JAVA API that comes with thousands of methods. Explaining all is not possible. Go to open market in a city book selling area and find for your taste. I am not having good idea on latest books on stands.

  2. dhars

    “Playing media player while doing some activity on the system like typing a document” – whether it comes under multiprocessing or multi threading

  3. Khan

    Sir ,
    I’m unable to understand this line “IllegalStateException : It is thrown when a thread is tried to start that is already started.” From the above passage .

    i think it should start like this . Once Thread dies we can’t make use of that thread if we want the dead thread to run then i think an IllegalStateException will be thrown .

    correct me sir if i’m wrong please.

Leave a Reply

Your email address will not be published. Required fields are marked *