Java Multithreading

October 21, 2018 Off By ravi24kant

Java Multithreading

To understand Java Multithreading, as you can eat food while talking on all the phones and watch TV as well. Our brain is programmed in such a way that we can do many things together. It is called multitasking. Similarly, any program of java can complete multiple tasks simultaneously. This feature of Java is called Java Multithreading. For example, when your program is waiting for the user input, then at this time, another can complete another task. As soon as the connectivity can be done from the database or a file can be uploaded.

A Java Multithreading program has more than 2 or more parts. These parts can be executed simultaneously. Every part is called a thread. Every thread is responsible for completing a unique task. All of these threads are in main thread. The main thread of your program starts with the main () method. Every thread has a life cycle. In this life cycle, thread passes through many stages. When you create an object of thread class, first one thread comes into the new state. After this, when the method is called the start () method on the thread object, the thread is reached in the runnable state. After this, when the run () method call occurs in thread running state. After this, if the thread has to wait for execution, the thread reaches the waiting state. And when the execution of the thread is completed then the thread reaches the destroyed stage.

Java Multithreading can be implemented in 2 ways.

  1. In the first way you extend the thread class.
  2. In the second way you implement the Runnable interface.

When you want to use other methods in addition to the run () method, you can extend the thread class. And if you just want to override run (), then you can implement Runnable interface

Java Multithreading : Thread Class Implementation

Thread class provides many methods. But you do not need to override all of
these methods to implement thread class. You can just override the run ()
method. Some important methods of thread class are being given below.

Method                             Description
getName()                        Returns name of thread.
getPriority()                     Returns priority of thread.
isAlive()                            Returns true if thread is active.
join()                                 Joins two threads.
run()                                 Threads task is defined in this method.
sleep()                              Make a sleep for give time in milliseconds.
start()                               Starts a thread

First you have to extend the thread class. After this you override the run method in your class. If you want you can override even more methods. But it is necessary to override the run () method. After this you create your own object of this class. When you create an object, you can also pass an unique name for the thread.
After creating the object, you call the start () method on that object. start () method automatically calls the run () method, and your thread starts execution. You create as many objects as you want to create threads

class JavaMultiThreading extends thread
{
public static void main(String args[])
{
JavaMultiThreading jm = new JavaMultiThreading ();
jm.start();
}
Public void run()
{
System.out.println(“Thread is running”);
}
}

Java Multithreading : Runnable Interface Implementation

There is only one method in the Runnable interface. You have to provide the definition of run () method in your class. First you create a class, which executes the run () method. After this, you create an object of the Thread
class. In the object of this thread class, you pass the name of object and thread to the class that executes the Runnable interface. And then you call the start () method on the object of thread class.

When you pass the object object of a class that is executing the Runnable
interface into the object of the cast class, the object of the thread class points
to the object of the same class. An example of this is being given below.

class JavaMultiThreading implements Runnable
{

JavaMultiThreading()
{
Thread t = new Thread(this,JavaThreading );
t.start();
}
public void run()
{

System.out.println(“Thread is running”);

}
public static void main(String args[])
{

JavaMultiThreading jmt = new JavaMultiThreading();

}

}

Java Multithreading : Synchronization

When more than 2 or 2 thread wants access to a single resource, then the conflict arises. To avoid this conflict, you use a technique. With this technique, one thread can access the same thread at a time. This technique is called synchronization.

In the synchronization technique, you make a method synchronized. For this, you add a synchronized method before the definition of that method. Such as

synchronized void myMethod () {}

To understand the concept of synchronization, you must understand the concept of a monitor. With each object, its monitor is connected. When a thread calls to a thread synchronized method, it enters the thread monitor. As soon as the thread enters the monitor, it is locked on the resource. As long as this thread does not free up this resource, no other thread can access this resource.

There are 2 ways to make any resource synchronized. One, as I told you, you can put a synchronized keyword next to the method and make it synchronized. In another way you create a synchronized block. And in this
block, call the methods of that class, which you want to make synchronized

An example of this is being given below

Synchronized(s1) // s1 is the object of class of which methods you are
going to call
{
// call here methods that you want to make synchronized
}