Thread Priority

23/11/2009 17:16

 

class Demo implements Runnable
{
int i=0;
Thread t;
private volatile boolean running=true;
public Demo(int p)
{
t=new Thread(this);
t.setPriority(p);
}
public void run()
{
while(running)
i++;
}
public void stop()
{
running=false;
}
public void start()
{
t.start();
}
}
class TPriority
{
public static void main(String args[])
{
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
Demo hi=new Demo(Thread.NORM_PRIORITY+2);
Demo lo=new Demo(Thread.NORM_PRIORITY-2);
lo.start();
hi.start();
try
{
Thread.sleep(10000);
}
catch(InterruptedException e)
{
System.out.println("Main thread interrupted");
}
lo.stop();
hi.stop();
try
{
hi.t.join();
lo.t.join();
}
catch(InterruptedException e)
{
System.out.println("Interrupted Exception caught");
}
System.out.println("Low priority Thread:"+lo.i);
System.out.println("High priority Thread:"+hi.i);
}
}

 

Back

Search site

Copyright © 2012 Dadaso Zanzane. All rights reserved.