Multi threading in JAVA

Reading Time: 3 minutes
Java Threads - Creating Threads and Multithreading in Java | by Swatee  Chand | Edureka | Medium

In this blog we will understand how Multi threading works in JAVA and why is it so Important to understand?

What is Multithreading?

Multithreading in Java is a process of executing two or more threads simultaneously to maximum utilisation of CPU. Multi-threaded applications execute two or more threads run concurrently. Hence, it is also known as Concurrency in Java.

Advantages of multithread:

  • The users are not blocked because threads are independent, and we can perform multiple operations at times
  • As such the threads are independent, the other threads won’t get affected if one thread meets an exception.
  • Program structure simplification. Threads can be used to simplify the structure of complex applications, such as server-class and multimedia applications. Simple routines can be written for each activity, making complex programs easier to design and code, and more adaptive to a wide variation in user demands.
  • Improved server responsiveness. Large or complex requests or slow clients don’t block other requests for service. The overall throughput of the server is much greater.

Life cycle of a Thread

Thread Life Cycle
  • New – A thread that has not yet started is in this state.
  • Runnable – A thread executing in the Java virtual machine is in this state.
  • Blocked – A thread that is blocked waiting for a monitor lock is in this state.
  • Waiting – A thread that is waiting indefinitely for another thread to perform a particular action is in this state.
  • Dead/Terminated – A thread that has exited is in this state.

A thread can be in only one state at a given point in time.

We can create threads in java by extending the java.lang.Thread class or by implementing the java.lang.Runnable interface. Both the implementation overrides the run() method to do so. Both the implementations can be used according to the use case of the class. 

Creating Thread Using Thread Class

@Component
public class MultiThreadingExample extends Thread{

    private final static Logger log = LoggerFactory.getLogger(MultiThreadingExample.class);
    @Override
    public void run() {
        log.info("This class extends Thread class for multithreading");
    }

}

Synchronization

  • Multithreading introduces asynchronous behavior to the programs. If a thread is writing some data another thread may be reading the same data at that time. This may bring inconsistency.
  • When two or more threads need access to a shared resource there should be some way that the resource will be used only by one resource at a time. The process to achieve this is called synchronization.
  • To implement the synchronous behavior java has synchronous method. Once a thread is inside a synchronized method, no other thread can call any other synchronized method on the same object. All the other threads then wait until the first thread come out of the synchronized block.
  • When we want to synchronize access to objects of a class which was not designed for the multithreaded access and the code of the method which needs to be accessed synchronously is not available with us, in this case we cannot add the synchronized to the appropriate methods. In java we have the solution for this, put the calls to the methods (which needs to be synchronized) defined by this class inside a synchronized block in following manner.

Checkout our other blogs on java and java on Knoldus blogs.

Checkout our templates here.

This image has an empty alt attribute; its file name is footer-2.jpg

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading