Saturday, 12 October 2013

Program to demonstrate Thread Priority

//Program to demonstrate Thread Priority
//Developed by Ahlam Ansari
import java.util.*;
   
class Thread1 extends Thread
{
    int n;
    Thread1 (int n)
    {
        this.n=n;
    }

    public void run()
    {
        for(int i=1;i<=n;i++)
        {
            System.out.println("Thread 1");   
        }
    }
}

class Thread2 extends Thread
{
    int n;
    Thread2 (int n)
    {
        this.n=n;
    }

    public void run()
    {
        for(int i=1;i<=n;i++)
        {
            System.out.println("Thread 2");   
        }
    }
}

class Thread3 extends Thread
{
    int n;
    Thread3 (int n)
    {
        this.n=n;
    }

    public void run()
    {
        for(int i=1;i<=n;i++)
        {
            System.out.println("Thread 3");   
        }
    }
}

class ThreadPriority
{
    public static void main(String ahlam[])
    {
        int n;
        Scanner d=new Scanner(System.in);
        System.out.println("Enter the number");
        n=d.nextInt();
        Thread1 th1=new Thread1(n);
        Thread2 th2=new Thread2 (n);
        Thread3 th3=new Thread3 (n);
        th1.setPriority(Thread.MAX_PRIORITY);
        th2.setPriority(Thread.MIN_PRIORITY);
        th3.setPriority(Thread.NORM_PRIORITY);
        th1.start();
        th2.start();
        th3.start();
    }
}

/*
Output
C:\Users\Ahlam\Google Drive\My Lectures\Fall\OOPM\Programs>javac ThreadPriority.java

C:\Users\Ahlam\Google Drive\My Lectures\Fall\OOPM\Programs>java ThreadPriority
Enter the number
5
Thread 1
Thread 1
Thread 1
Thread 1
Thread 1
Thread 2
Thread 2
Thread 2
Thread 2
Thread 2
Thread 3
Thread 3
Thread 3
Thread 3
Thread 3

C:\Users\Ahlam\Google Drive\My Lectures\Fall\OOPM\Programs>
*/

No comments:

Post a Comment