(P - 1 - 4)
Write a Java program to create five threads with different priorities. Send 1st two highest priority thread to sleep mode and check the aliveness of the threads and check which thread is long lasting.
=========================================
import java.lang.*;
class ThreadPriority extends Thread
{
public static void main(String[]args) throws InterruptedException
{
ThreadPriority t1 = new ThreadPriority();
ThreadPriority t2 = new ThreadPriority();
ThreadPriority t3 = new ThreadPriority();
ThreadPriority t4 = new ThreadPriority();
ThreadPriority t5 = new ThreadPriority();
t1.setPriority(9);
t2.setPriority(8);
t3.setPriority(5);
t4.setPriority(3);
t5.setPriority(2);
t1.sleep(1000);
if (t1.isAlive())
System.out.println("Thread 1 is alive");
else
System.out.println("Thread 1 is not alive");
t2.sleep(1000);
if (t2.isAlive())
System.out.println("Thread 2 is alive");
else
System.out.println("Thread 2 is not alive");
t3.start();
if (t3.isAlive())
System.out.println("Thread 3 is alive");
else
System.out.println("Thread 3 is not alive");
t4.start();
if (t4.isAlive())
System.out.println("Thread 4 is alive");
else
System.out.println("Thread 4 is not alive");
t5.start();
if (t5.isAlive())
System.out.println("Thread 5 is alive");
else
System.out.println("Thread 5 is not alive");
}
}
Write a Java program to create five threads with different priorities. Send 1st two highest priority thread to sleep mode and check the aliveness of the threads and check which thread is long lasting.
=========================================
import java.lang.*;
class ThreadPriority extends Thread
{
public static void main(String[]args) throws InterruptedException
{
ThreadPriority t1 = new ThreadPriority();
ThreadPriority t2 = new ThreadPriority();
ThreadPriority t3 = new ThreadPriority();
ThreadPriority t4 = new ThreadPriority();
ThreadPriority t5 = new ThreadPriority();
t1.setPriority(9);
t2.setPriority(8);
t3.setPriority(5);
t4.setPriority(3);
t5.setPriority(2);
t1.sleep(1000);
if (t1.isAlive())
System.out.println("Thread 1 is alive");
else
System.out.println("Thread 1 is not alive");
t2.sleep(1000);
if (t2.isAlive())
System.out.println("Thread 2 is alive");
else
System.out.println("Thread 2 is not alive");
t3.start();
if (t3.isAlive())
System.out.println("Thread 3 is alive");
else
System.out.println("Thread 3 is not alive");
t4.start();
if (t4.isAlive())
System.out.println("Thread 4 is alive");
else
System.out.println("Thread 4 is not alive");
t5.start();
if (t5.isAlive())
System.out.println("Thread 5 is alive");
else
System.out.println("Thread 5 is not alive");
}
}
No comments :
Post a Comment