• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Thread

 
Ranch Hand
Posts: 290
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 625
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic