Thread Creation Concept Simplified in Java



Multithreading corresponds to multiple flows of control. It is a conceptual paradigm where we can divide a program into two or more processes which can run in parallel.Java allows us to have inbuilt processes where more than one process can be executed concurrently within a single program.Java threads are lightweight, meaning they can run in the same memory space. They can communicate themselves(one object of one thread class can call a method of another thread without any overhead of operating system). Threads can execute independently and simultaneously. So java handles multiple tasks simultaneously using multi threaded way.This feature enables to finish tasks before even beginning another.
Java coding is mostly sequential single flow execution(single threaded programming). A thread is a single flow of control having a beginning a body and end.A thread executes commands sequentially.
The ability to support multi threads is referred as concurrency.Since the threads in java are sub program of the main application and share the same memory space.Even though threads appears to run parallel , it is  actually sequential but very fast switching of control between threads.

Once initiated by main program, Thread A, Thread B, and Thread C run independently , concurrently and share the system resources.

Java provides two to way to create a thread.

  • Sub classing the Thread class. Here we define a class and extends to Thread class, then override the run() method.
  • Implementing the runnable interface. Here we implement the runnable interface and define the run() method.
Sub Classing to the Thread Class:(Read about the Thread class.)
Steps:
  1. We need to declare the class as extending to Thread class. 
  2. Override the run() method in the subclass which is responsible for executing the sequence of code that the thread will execute.
  3. Create a thread object and call the start() method to initiate the thread execution. 
Example:

class MyThread extends Thread{
//MyThread is a new Thread which extends to Thread Class. This subclass will contain a method called run().run() 
//contains the actual task what thread should perform.
    private String whichThread;
    private int delay;
    MyThread(String myString,int timeOut)
    {
        whichThread=myString;
        delay=timeOut;
    }
//Main method to construct thread and the only method in which thread's behavior can be written. 
//Here we need to override the run() method.
    public void run()
    {
        try{
            sleep(delay);
        }
    catch(InterruptedException e)
    {
        System.out.println("A thread is interrupted");
    }
    finally{
        System.out.println("Hello "+ whichThread+" slept "+delay);
    }    
    }
}
public class TestThread{
    public static void main(String []args){
      MyThread t1,t2,t3;
//creating the object of the class we created earlier. The state of the threads will be newborn
      t1=new MyThread("Thread 1",10);
      t2=new MyThread("Thread 2",20);
      t3=new MyThread("Thread 3",30);
//start() method to start the threads that will invoke the run() method . This will cause the threads to go to runnable state.Java run time will schedule the threads to run by invoking it's run method.
      t1.start();
      t2.start();
      t3.start();
      try{
          t1.join();
          t2.join();
          t3.join();
      }
      catch(InterruptedException e)
      {
          
      }
     }
}
 
the output of the code:
$javac TestThread.java
$java -Xmx128M -Xms16M TestThread
Hello Thread 1 slept 10
Hello Thread 2 slept 20
Hello Thread 3 slept 30
Another Example:
Once we initiate a thread with new and started them, they are running concurrently on their own. The output are not specially sequential. They just do not follow any specific order. Once the thread has been started , we can not decide in which order they may execute the statements.

class A extends Thread{
public void run()
{
for(int i=1;i<5;i++)
{
System.out.println("From Thread A "+i);
}
System.out.println("Exit from Thread A ");
}
}
class B extends Thread{
public void run()
{
for(int j=1;j<5;j++)
{
System.out.println("From Thread B "+j);
}
System.out.println("Exit from Thread B ");
}
}
class C extends Thread{
public void run()
{
for(int k=1;k<5;k++)
{
System.out.println("From Thread C "+k);
}
System.out.println("Exit from Thread C ");
}
}
public class ThraedTest{
public static void main(String args[])
{
new A().start();
new B().start();
new C().start();
}
}
 
the output of the code:

$javac ThraedTest.java
$java -Xmx128M -Xms16M ThraedTest
From Thread A 1
From Thread A 2
From Thread A 3
From Thread A 4
Exit from Thread A
From Thread B 1
From Thread B 2
From Thread B 3
From Thread B 4
Exit from Thread B
From Thread C 1
From Thread C 2
From Thread C 3
From Thread C 4
Exit from Thread C
$javac ThraedTest.java
$java -Xmx128M -Xms16M ThraedTest
From Thread A 1
From Thread A 2
From Thread B 1
From Thread C 1
From Thread A 3
From Thread A 4
Exit from Thread A
From Thread B 2
From Thread B 3
From Thread B 4
Exit from Thread B
From Thread C 2
From Thread C 3
From Thread C 4
Exit from Thread C

Using Runnable Interface: Read about the runnable interface:
The second way to create Thread is to make use of the runnable interface. With this approach, we need to implement the runnable interface to the class. Runnable interface is already defined in java.lang package with a single method called- run().This run() method is required for implementing threads in our program. While implementing a new thread, a new object will be instantiated from this runnable interface as the target of our thread, meaning the thread will look for the code for run() method within the object's class instead of the Thread class.
Steps:

  1. Declare the class as implementing the Runnable interface.
  2. Implement the run() method.
  3. Create a thread by defining an object that is instantiated from this "runnable" class as the target of the thread.
  4. Call the thread's start() method to run the thread.

Example:

class Pen implements Runnable{
    private int price;
    private String name;
    Pen(int price,String name)
    {
        this.price=price;
        this.name=name;
    }
    public void run()
    {
        try{
            Thread.sleep(price*1000);
            System.out.println("A pen "+name+" price is "+price);
        }
    catch(InterruptedException e)
    {
        System.out.println("Pen thread is interrupted");
    }
       
    }
}
class Pencil implements Runnable{
    private int price;
    private String name;
    Pencil(int price,String name)
    {
        this.price=price;
        this.name=name;
    }
    public void run()
    {
        try{
            Thread.sleep(price*2000);
            System.out.println("A Pencil "+name+" price is "+price);
        }
    catch(InterruptedException e)
    {
        System.out.println("Pencil thread is interrupted");
    }
       
    }
}
public class TestThreadRunnable{
    public static void main(String []args){
      Pen myPen=new Pen(2,"Lammy");
      Thread t1=new Thread(myPen);
      t1.start();
      Pencil myPencil=new Pencil(3,"Nataraj");
      Thread t2=new Thread(myPencil);
      t2.start();
      
      try{
          t1.join();
          t2.join();
          
      }
      catch(InterruptedException e)
      {
          
      }
     }
}
 
the output of the code:
$javac TestThreadRunnable.java
$java -Xmx128M -Xms16M TestThreadRunnable
A pen Lammy price is 2
A Pencil Nataraj price is 3
A class instance with run() method defined within, must be passed in as as argument in creating the thread instance so that when the start() mthod of this instance is called, java runtime knows which run method to execute. This alternative method of creating a thread is useful when the class defining the run() method needs to be a subclass of another subclass. The class can inherit all the data method of the super class.
Advantage:

  1. Program with multiple threads will result in better utilization of System resources(CPU).
  2. This feature improves the interactive performance of the graphical application

Thread Creation Concept Simplified in Java Thread Creation Concept Simplified in Java Reviewed by Animesh Chatterjee on September 24, 2018 Rating: 5

No comments:

Powered by Blogger.