java22

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Q. Create three threads and run these threads according to set priority.

public class PriorityExample {

public static void main(String[] args ) {

Thread thread1=new MyThread("Thread 1");

Thread thread2=new MyThread("Thread 2");

Thread thread3=new MyThread("Thread 3");

thread1.setPriority(Thread.MAX_PRIORITY);

thread2.setPriority(Thread.NORM_PRIORITY);

thread3.setPriority(Thread.MIN_PRIORITY);

thread1.start();

thread2.start();

thread3.start();

public static class MyThread extends Thread

public MyThread(String name){

super(name);

public void run(){

System.out.println(getName()+"running");

for(int i=1;i<=5;i++)

System.out.println(getName()+"counting"+i);

try{

Thread.sleep(1000);

}
catch(InterruptedException e){

e.printstackTrace();

System.out.println(getName()+"finished");

You might also like