MCS024 - Assignment 5(c) - Write a java program to create two threads with different priority.

class ThreadPrio implements Runnable
{
public long count=0;
    Thread t;
    private volatile boolean running=true;
    
    public ThreadPrio(int p)
    {
    t=new Thread(this);
    t.setPriority(p);
    }
    public void run()
    {
    while(running)
       count++;
    }
    public void start()
    {
    t.start();
    }
    public void stop()
    {
    running=false;
   
    }

public static void main(String[] args) throws InterruptedException
{
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
ThreadPrio t7=new ThreadPrio(7);
ThreadPrio t5=new ThreadPrio(5);
ThreadPrio t3=new ThreadPrio(3);
ThreadPrio t1=new ThreadPrio(1);
    t1.start();
    t3.start();
    t5.start();
    t7.start();
    System.out.println("Main going to sleep for 10 secs. Plz wait patiently.");
    Thread.sleep(10000);
    t1.stop();
    t3.stop();
    t5.stop();
    t7.stop();
    
    t1.t.join();
    t3.t.join();
    t5.t.join();
    t7.t.join();
    
    System.out.println("Thread with Priority 1 :"+t1.count);
    System.out.println("Thread with Priority 3 :"+t3.count);
    System.out.println("Thread with Priority 5 :"+t5.count);
    System.out.println("Thread with Priority 7 :"+t7.count);
    

}     



}


No comments

Powered by Blogger.