File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes PRoblem On threads Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply 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
Saloon Keeper

Joined: Aug 03, 2008
Posts: 9189
    
    2

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
Saloon Keeper

Joined: Aug 03, 2008
Posts: 9189
    
    2

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 agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: PRoblem On threads
 
Similar Threads
Thread with same instance as lock Query
Threads using two different instances Problem
Threads
Threads and join()
Threads with separate lock Problem