wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Thread" Watch "Thread" New topic
Author

Thread

nitin sharma
Ranch Hand

Joined: Feb 24, 2001
Posts: 290
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

Joined: Dec 16, 2000
Posts: 625
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

Joined: Feb 24, 2001
Posts: 290
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

Joined: Dec 16, 2000
Posts: 625
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

Joined: Feb 24, 2001
Posts: 290
sean,
windows platforms were originally preemptive ,but changed to time-sliced with the 1.0.2 relaese of jdk.
nitin sharma
Ranch Hand

Joined: Feb 24, 2001
Posts: 290
sean,i got your point.
Thank's for the angry stuff.
Sean Casey
Ranch Hand

Joined: Dec 16, 2000
Posts: 625
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).]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread
 
Similar Threads
MG1 #54
sleep??
about thread.start() and thread.run()
Thread question: extending Thread class and start() 2 instances ?
Marcus Green Mock Exam 1