Multi Threading
Multi Threading
System.out.println("Thread1 is running");
}
}
System.out.println("Thread2 is running");
}
}
class GFG {
// Main method
public static void main(String[] args)
{
System.out.println("Thread1");
try {
// Making the thread pause for a certain
// time using sleep() method
Thread.sleep(1000);
}
catch (Exception e) {
}
}
}
}
try {
Thread.sleep(1000);
}
catch (Exception e) {
}
}
}
}
}
}
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 ();
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());
}
}
threadObject1.setPriority(4);
threadObject2.setPriority(Thread.MAX_PRIORITY);
threadObject1.start();
threadObject2.start();
}
}
Thread Priority #2
// Importing the required classes
import java.lang.*;
// 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");
}