❮ Previous Next ❯

Java Thread Priority

Let's see a program for use of thread priorities :

class A extends Thread
{
public void run( )
{
int p;
for(p=1;p<=5;p++)
{
System.out.println("p="+p);
}
}
}
class B extends Thread
{
public void run( )
{
int q;
for(q=1;q<=5;q++)
{
System.out.println("q="+q);
}
}
}
class C extends Thread
{
public void run( )
{
int r;
for(r=1;r<=5;r++)
{
System.out.println("r="+r);
}
}
}
class Gyana
{
public static void main(String args[ ])
{
A t1=new A( );
B t2=new B( );
C t3=new C( );
t3.setPriority(Thread.MAX_PRIORITY);
t2.setPriority(2);
t1.setPriority(Thread.MIN_PRIORITY);
t1.start( );
t2.start( );
t3.start( );
}
}
Output

r=1
r=2
r=3
r=4
r=5
p=1
p=2
p=3
p=4
p=5
q=1
q=2
q=3
q=4
q=5

In this program the output represents the effect of assigning higher priority to a thread. Although thread A started first, but due to higher priority of thread B, B have preempted it and started printing the output first. At the same time, thread C that have assigned highest priority takes control over the other two threads. The thread A is the last to execute.

❮ Previous Next ❯



  Tutorials

Popular Links

Contact Us

address Plot No-741,2ND Floor
Opp. Bhagabati Temple,Jayadev Vihar
      Bhubaneswar-751013
      Email: info@silantechnology.com
      Phone: 0674-2361252
address
39877, sundale dr, apt#101, Fremont, California 94538, USA
       Phone: +1(262)388-7619

facebook twitter google linked in

© 2018 Silan Technology. All Rights Reserved