Example #1 Create Thread class
class MyThread1 extends Thread {
public void run()
{
System.out.println("Thread1 is running");
}
}
class MyThread2 extends Thread {
public void run()
{
System.out.println("Thread2 is running");
}
}
class GFG {
// Main method
public static void main(String[] args)
{
MyThread1 obj1 = new MyThread1();
MyThread2 obj2 = new MyThread2();
obj1.start();
obj2.start();
}
}
import java.io.*;
Example #2: Implements Runnable
import java.util.*;
class MyThread1 implements Runnable {
public void run()
{
for (int i = 0; i < 5; i++) {
System.out.println("Thread1");
try {
// Making the thread pause for a certain
// time using sleep() method
Thread.sleep(1000);
}
catch (Exception e) {
}
}
}
}
class MyThread2 implements Runnable {
public void run()
{
for (int i = 0; i < 5; i++) {
System.out.println("Thread2");
try {
Thread.sleep(1000);
}
catch (Exception e) {
}
}
}
}
public class GFG {
// Main driver method
public static void main(String[] args)
{
Runnable obj1 = new MyThread1();
Runnable obj2 = new MyThread2();
// Creating reference of thread class
// by passing object of Runnable in constructor of
// Thread class
Thread t1 = new Thread(obj1);
Thread t2 = new Thread(obj2);
// Starting the execution of our own run() method
// in the classes above
t1.start();
t2.start();
}
}
class A1 extends Thread
{
public void run ()
{
System.out.println ("Thread A");
System.out.println ("i in Thread A ");
for (int i = 1; i <= 5; i++)
{
System.out.println ("i = " + i);
try
{
Thread.sleep (1000);
}
catch (InterruptedException e)
{
e.printStackTrace ();
}
}
System.out.println ("Thread A Completed.");
}
}
class B extends Thread
{
public void run ()
{
System.out.println ("Thread B");
System.out.println ("i in Thread B ");
for (int i = 1; i <= 5; i++)
{
System.out.println ("i = " + i);
}
System.out.println ("Thread B Completed.");
}
}
public class ThreadLifeCycleDemo
{
public static void main (String[]args)
{
// life cycle of Thread
// Thread's New State
A1 threadA = new A1 ();
B threadB = new B ();
System.out.println("Thread state before sleep: " + threadA.getState());
System.out.println("Thread state before sleep: " + threadB.getState());
// Both the above threads are in runnable state
// Running state of thread A & B
threadA.start ();
System.out.println("Thread state After sleep: " + threadA.getState());
System.out.println("after starting thread isAlive: "+ threadA.isAlive());
// Move control to another thread
threadA.yield ();
threadA.join();
System.out.println("after starting thread isAlive: "+ threadB.isAlive());
// Blocked State of thread B
try
{
threadA.sleep (1000);
}
catch (InterruptedException e)
{
e.printStackTrace ();
}
threadB.start ();
System.out.println ("Main Thread End");
System.out.println("after starting thread isAlive: "+ threadB.isAlive());
}
}
‘Thread Priority #1
class SampleThread extends Thread{
public void run() {
System.out.println("Inside SampleThread");
System.out.println("Current Thread: " + Thread.currentThread().getName());
}
}
public class My_Thread_Test {
public static void main(String[] args) {
SampleThread threadObject1 = new SampleThread();
SampleThread threadObject2 = new SampleThread();
threadObject1.setName("first");
threadObject2.setName("second");
threadObject1.setPriority(4);
threadObject2.setPriority(Thread.MAX_PRIORITY);
threadObject1.start();
threadObject2.start();
}
}
Thread Priority #2
// Importing the required classes
import java.lang.*;
public class ThreadPriorityExample extends Thread
{
// Method 1
// Whenever the start() method is called by a thread
// the run() method is invoked
public void run()
{
// the print statement
System.out.println("Inside the run() method");
}
// the main method
public static void main(String argvs[])
{
// Creating threads with the help of ThreadPriorityExample class
ThreadPriorityExample th1 = new ThreadPriorityExample();
ThreadPriorityExample th2 = new ThreadPriorityExample();
ThreadPriorityExample th3 = new ThreadPriorityExample();
System.out.println("Priority of the thread th1 is : " + th1.getPriority());
System.out.println("Priority of the thread th2 is : " + th2.getPriority());
System.out.println("Priority of the thread th2 is : " + th2.getPriority());
th1.setPriority(6);
th2.setPriority(3);
th3.setPriority(9);
System.out.println("Priority of the thread th1 is : " + th1.getPriority());
System.out.println("Priority of the thread th2 is : " + th2.getPriority());
System.out.println("Priority of the thread th3 is : " + th3.getPriority());
System.out.println("Currently Executing The Thread : " + Thread.currentThread().getName());
System.out.println("Priority of the main thread is : " + Thread.currentThread().getPriority());
// Priority of the main thread is 10 now
Thread.currentThread().setPriority(10);
System.out.println("Priority of the main thread is : " + Thread.currentThread().getPriority());
}
}