Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread

 
nitin sharma
Ranch Hand
Posts: 290
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class Rpcraven
{
public static void main(String argv[]){
Pmcraven pm1 = new Pmcraven("one");
pm1.start();
System.out.println("what the hell are u doing");
Pmcraven pm2 = new Pmcraven("two");
pm2.start();
}
}
class Pmcraven extends Thread
{
private String sTname="";
Pmcraven(String s)
{
sTname = s;
}
public void run()
{
for(int i =0; i<5 ; i++){
try
{
sleep(1000);
}
catch(InterruptedException e)
{
}
yield();
System.out.println(sTname);
}
}
}
In the above given code,i have created two thread's named one and two.My question is when the thread "one" yields for the first time,second thread named "two" should start running immediately.Initial two line's of output should be "two","one" not "one","two".Please throw some light on it.
 
Sean Casey
Ranch Hand
Posts: 625
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Without even looking at your code, you have to understand that thread behaviour will vary on different os's. Some systems use time-splicing (I think Solaris) while other's use priorities to see who runs when.
 
nitin sharma
Ranch Hand
Posts: 290
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi sean,
window's also have time slice approach,my question is when the yield is executed for the first time then any other thread in the ready state should execute.But in the above given code that is not happening.I am printing something which is just after the yield method as a result it should not be printed first which it is.I expect the other thread to print first.
 
Sean Casey
Ranch Hand
Posts: 625
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why on earth should two be printed first? the first thread yields, then the second thread yields, and the first continues by printing out is sTname, and vice versa.
Here is the output on my linux box.
what the hell are you doing
one
two
one
two
one
two
one
two
one
two
P.S. I always thought that Windows scheduler was preemptive.


[This message has been edited by Sean Casey (edited May 05, 2001).]
[This message has been edited by Sean Casey (edited May 05, 2001).]
 
nitin sharma
Ranch Hand
Posts: 290
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sean,
windows platforms were originally preemptive ,but changed to time-sliced with the 1.0.2 relaese of jdk.
 
nitin sharma
Ranch Hand
Posts: 290
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sean,i got your point.
Thank's for the angry stuff.
 
Sean Casey
Ranch Hand
Posts: 625
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What angry stuff? If I came across as being angry, then I apologize, but I'm merely trying to help you by answering your questions. If you're referring to my "Why on earth" comment, it's because I'm a sarcastic person, and the flow of the program clearly demonstrates that the first thread will be printed then the second and not vice versa. I'm not trying to offend anyone. I'm only trying to help.
P.S. the "what the hell are you doing" isn't my comment, it comes straight from your own program.

[This message has been edited by Sean Casey (edited May 05, 2001).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic