This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes PRoblem On threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "PRoblem On threads" Watch "PRoblem On threads" New topic
Author

PRoblem On threads

Mohit G Gupta
Ranch Hand

Joined: May 18, 2010
Posts: 634




Is the output possible:Thread-0 runs before main
THread-0
A
main
B

i had ran this code 10 times,still the same output


OCPJP 6.0 93%
OCPJWCD 5.0 98%
Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
Not sure what you mean by "Is the output possible: Thread-0 runs before main" - you're observing it, so obviously it's possible.

Since the "B" thread is put to sleep, the JVM will choose some other thread to run, and the "A" thread is the only other user thread in existence - so this behavior is to be expected.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9399
    
  20

Thread-0 cannot print anything before main method because of the while loop in the GotIT method which will only end when the main method sets x to 1...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

mohitkumar gupta wrote:

Is the output possible:Thread-0 runs before main
THread-0
A
main
B

You can't get this output! There is a endless loop on the GotIT() method!


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9399
    
  20

There is a endless loop on the GotIT() method!

That is not an endless loop. That loop only terminates when x is not 0...
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Ankit Garg wrote:
That is not an endless loop. That loop only terminates when x is not 0...

Yea, endless loo, if x won't be reset with the value 1 by the main thread! Thanks!
 
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com
 
subject: PRoblem On threads
 
It's not a secret anymore!