class newthread extends
Thread {
String name;
Thread t;
public newthread(String threadname)
{
name=threadname;
t=new Thread(this,name);
System.out.println("new Thread:"+t);
t.start();
}
public void run()
{
try
{
for(int i=5;i>0;i--)
{
System.out.println(name+":"+i);
Thread.sleep(1000);
}
}
catch(InterruptedException e)
{
System.out.println(name+"interrupted");
}
System.out.println(name+"exciting:");
}
}
class thread
{
public static void main(String[]args)
{
new newthread("one");
new newthread("two");
newthread t=new newthread("three");
t.setPriority(10);
try
{
Thread.sleep(10000);
}
catch(InterruptedException e)
{
System.out.println("MAIN THREAD INTERRUPTED");
}
}
}
The output follow's.
new Thread:Thread[one,5,main]
new Thread:Thread[two,5,main]
new Thread:Thread[three,5,main]
two:5
one:5
three:5
two:4
one:4
three:4
two:3
three:3 //look at this line,thread three overtaking the other thread's or u can say preempting the lower priority thread's
one:3
two:2
three:2
one:2
two:1
three:1
one:1
twoexciting:
threeexciting:
oneexciting:
In the above code i expected the third thread to take the largest priorty and it should have completed first,is it possible that thread scheduler may not run the highest priority thread first?.
In any situation,can a thread with higher priority stop's the other thread from executing?.
I have one more question what happen's when all the thread's have equal priority .Is IT possible to determine whuch thread will run first or after registration it is entirely upto thread scheduler to decide which thread should run first.Please throw a mashal on it.