wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Exam LAb Question 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 "Exam LAb Question on THreads" Watch "Exam LAb Question on THreads" New topic
Author

Exam LAb Question on THreads

Mohit G Gupta
Ranch Hand

Joined: May 18, 2010
Posts: 634



Is it possible that main thread runs the line 1 and line2 and line3 first and then line 4.

b thread may start after the main thread


OCPJP 6.0 93%
OCPJWCD 5.0 98%
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

The code is incomplete. As for your question, its possible that main thread executes before any other thread that you start.

[Edit] Now that you've posted the complete code, yes its possible that main thread executes before b thread (even though the possibility is very less but its possible). If b thread starts before the main thread, there will be a deadlock..


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Mohit G Gupta
Ranch Hand

Joined: May 18, 2010
Posts: 634


UPDATED the code

As for your question, its possible that main thread executes before any other thread that you start...


THe answer is exam lab is that
there would be no output .
As when thread b starts ,it acquires lock on a.then,when it goes to it's doTask2 method,it joins and waits till thread main completes.
Thread main too needs lock on a to call line2.

The case when main thread runs before b thread is not taken into account
So, answer in Exam Lab is wrong
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Did you execute the code? There is a sleep() method!


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Mohit G Gupta
Ranch Hand

Joined: May 18, 2010
Posts: 634

i know that Thread.sleep(1000)
Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds. The thread does not lose ownership of any monitors.

i ran the program and no output appears

but ,i still have one confusion that
what if


are executed first than any other code including Thread.sleep(1000) in main thread
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

mohitkumar gupta wrote:i know that Thread.sleep(1000)

but ,i still have one confusion that
what if


are executed first than any other code including Thread.sleep(1000) in main thread

How can the main thread cross without executing the sleep() method?
Mohit G Gupta
Ranch Hand

Joined: May 18, 2010
Posts: 634

ok,i got it
thanks Abimaran
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

There can be output from the program. Even if main method sleeps for 1 second, the other thread might not get an opportunity to execute. The chances are very very low but its possible...
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Ankit Garg wrote:There can be output from the program. Even if main method sleeps for 1 second, the other thread might not get an opportunity to execute. The chances are very very low but its possible...

Agreed! But, these kind of question won't be in the exam, for sure, you can predict the output without this kind little possibility!
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

I would say you CAN face these kind of questions on the exam. For the exam the answer would be that there would be either no output or "12" might be the output...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exam LAb Question on THreads
 
Similar Threads
examlab(question-53)-unreachable statement NAO?
threads question in examLab practice test 1
WHy isnt there an Output?
synchronized block
exam lab practice exam -1, question24 need justification?