• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

order of thread output

 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
//program 1
class ThreadTest14 extends Thread {
String name;
public static void main(String[] args) {
new ThreadTest14("first").start();
new ThreadTest14("second").start();
}
ThreadTest14(String s) {
name=s;
}

public void run() {
for (int i=0;i<2;i++) {
System.out.println(name + i);
}
}
}
For program 1, the order of output is uncertain. But no matter what the order is, "first 0" will be the first element, right?
//program 2
class ThreadTest14 extends Thread {
String name;
public static void main(String[] args) {
new ThreadTest14("first").start();
new ThreadTest14("second").start();
}
ThreadTest14(String s) {
name=s;
}

public void run() {
for (int i=0;i<2;i++) {
System.out.println(name + i);
try {
Thread.sleep(1);
}
catch(InterruptedException e) {}
}
}
}
For program 2, I think the uncertainty is avoided by using Thread.sleep()method no matter how short current thread will sleep and the output will always be:
first 0
second 0
first 1
second 1
Am I right? Please help.
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bin
I don't think you can make either of those assumptions. when a thread runs and in what oreder are mostly up to the OS. Even though you start 'first' first and 'second' second there is no guarantee that they will run in that order. Another process could run on your system that puts both in the ready state before they print anything then it's a guess as to which will be started first.
In a perfect world, doing it the way you have shown, yes, usually the order will be like you say it will. However, I wouldn't make any assumptions based on that because there are so many other things that could effect it.

Dave
 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bin and Dave,
A very challenging question. I tried in my Windows98 laptop, the result is always like that Bin mentioned no matter how long time i put in the method of sleep. So, could you make a generalization?
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It does depend on the OS. On a Unix box the output would be "first0, first1, second0, second1" or "second0, second1, first0, first1" - if the threads have equal priority (sleep() would make it first0,second0,first1,second1 or second0,first0,second1,first1... probably... other threads could interfere with this). On a Microsoft OS then Dave's right, you have no real way of knowing what the output will be. The first thread could be cut out as soon as it begins. Generally though, the output would be as you presumed. Try it with a much bigger loop and you should see the numbers don't totally match up.
As far as the exam is concerned, you cannot tell one way or the other.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic