• 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

endless loop

 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ranchers,

What will be the output of this main method?






Yours,
Bu.
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

public static void main(String[] args) {
Runnable counter = new Runnable() {
public void run() {
System.out.println("running");
for(int i=1; i>0; i++)
;
System.out.println("ran.");

}
};
Thread thr = new Thread(counter);
thr.start();
thr.yield();
System.out.println("main ready");
}




From what I see

There are two possibilities

1) thread starts first, integer maxes out (incrementing beyond integers max value) and becomes negative, so the loop ends and then it prints 'running', then 'ran' first.
followed by 'main ready'
2) main completes first, then the thread starts and the same above logic happens.
so you will see 'main ready' first and 'running', and then 'ran' as output.

I only guessed this using my knowledge/understanding of Java didn't try it.

Is this right?
[ November 05, 2006: Message edited by: Ramkumar Sridharan ]
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi rancher,

the output is likely:

running
main ready
ran.


Because there are two traps in the code, not only one.

The first trap was - as Ramkumar already wrote - that this is no endless loop. I counts up to ~2.15 Billion, then we have an overflow, the i variable gets negative and the loop is over.

The second trap is the line
thr.yield();

yield() is a static method, that effects only the current Thread. And the current thread is not thr, thr never sees this line of code, the current thread here is the main thread always.

So there are two possibilities.
If thr has not started yet, the main thread does not yield, and prints out main ready first.
If thr has already started, it will print running first. As the loop takes some time, main ready will be printed out second and ran. last.

Without the yield, output is more likely
main ready
running
ran.

With the yield,
running
main ready
ran.


Because the counting thread takes some time, you almost never see main ready as last line of output.


Yours,
Bu.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic