As we previously discussed, Java can create a thread in two ways, either through Thread class or Runnable Interface.
Since, Runnable Interface consists a single method called run() and it is implemented by Thread Class itself. Even though if one creates a thread using Runnable, still requires to make an instance of the Thread class and the object required to pass to it as an argument.
Why we need to create Thread class object even after implementing Runnable Interface?
The answers is, start() method which is responsible for thread execution ( internally calling run method), registering a thread to thread Scheduler and more.
And this start() method resides in Thread class. So, this is the reason, why we need to create a Thread object separately.
Apart from start(), Thread class supports a bunch of useful methods that not only control thread’s behaviour but also in acquiring information of a thread such as thread name,its priority and more
Some of the important and mostly used thread class methods are discussed here
Program:
class ThreadExample extends Thread{ @Override public void run() { System.out.println("Child thread is about to sleep"); try { sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("I am child Thread "); } } public class Main { public static void main(String[] args) { ThreadExample thread1 = new ThreadExample(); thread1.start(); System.out.println("I am Main Thread "); } }
Output:
Child thread is about to sleep I am Main Thread I am child Thread
Program:
class ThreadExample extends Thread{ @Override public void run() { System.out.println("Child thread is printing"); for(int i=0;i<10;i++) { System.out.println(i); } try { sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } } } public class Main { public static void main(String[] args) throws InterruptedException { ThreadExample thread1 = new ThreadExample(); thread1.start(); thread1.join(); System.out.println("I will wait for child thread to join me"); System.out.println("Now Main thread is executing"); } }
Output:
Child thread is printing 0 1 2 3 4 5 6 7 8 9 I will wait for child thread to join me Now Main thread is executing
Program:
class ThreadExample extends Thread{ @Override public void run() { for(int i=0;i<10;i++) { yield(); System.out.println("I am child "+Thread.currentThread().getPriority()); } } } public class Main { public static void main(String[] args) { ThreadExample thread1 = new ThreadExample(); thread1.start(); System.out.println(" Main thread is executing"); for(int i=0;i<10;i++) { System.out.println("I am Main "+Thread.currentThread().getPriority()); } } }
Output:
Main thread is executing I am Main 5 I am child 5 I am Main 5 I am child 5 I am Main 5 I am child 5 I am Main 5 I am child 5 I am Main 5 I am child 5 I am Main 5 I am child 5 I am Main 5 I am child 5 I am Main 5 I am child 5 I am Main 5 I am child 5 I am Main 5 I am child 5
wait() and notify() are two companion methods that work together with the aim of providing more control over threads. They provide a way to achieve inter thread communication. These methods reside under Object class.
As name suggest,
Program:
class WaitExample extends Thread { @Override public void run() { print(); } synchronized void print() { System.out.println("THis is child "); notify(); } } public class Main { public static void main(String[] args) { WaitExample thread = new WaitExample(); thread.start(); synchronized (thread) { System.out.println("I am in synchronised :"); try { thread.wait(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("I am in invoked :"); }
Output:
Program:
class ThreadExample extends Thread{ @Override public void run() { for(int i=0;i<10;i++) { System.out.println("I am child "); } } } public class Main { public static void main(String[] args) { ThreadExample thread1 = new ThreadExample(); thread1.start(); System.out.println("Child thread is running or not :"+thread1.isAlive()); } }
Output:
I am child I am child I am child I am child I am child I am child I am child I am child I am child I am child Child thread is running or not :true
start(): start() method which is responsible for thread execution ( internally calling run method), registering a thread to thread Scheduler and more.
getName(): It is used to get the name of a thread
getPriority(): It is used to get the priority of a thread
class ThreadExample extends Thread{ @Override public void run() { System.out.println("This child"); System.out.println("Name : "+ Thread.currentThread().getName()); System.out.println("Priority : "+ Thread.currentThread().getPriority()); } } public class Main { public static void main(String[] args) { ThreadExample thread1 = new ThreadExample(); thread1.start(); System.out.println("Child thread is running or not :"+thread1.isAlive()); } }
Output:
This child Child thread is running or not :true Name : Thread-0 Priority : 5
Similarly,
setPriority(): to set a thread priority
setName(): to set a thread name
This post was last modified on December 27, 2020