my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Help in a question about Threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Help in a question about Threads" Watch "Help in a question about Threads" New topic
Author

Help in a question about Threads

Emanuele Ghe
Ranch Hand

Joined: Feb 04, 2009
Posts: 111
Hi,
I've found this question on www.scjptest.com:



The correct answer given by the site is


But in my opinion is wrong.

What e can say FOR SURE is that "End" will be printed AFTER "Run" because the main-thread joined the a-thread.

But we can't say anything about the ordering between "Start" and "Run", because it could happen that "Run" is printed before "Start" and vice-versa.
So a possible output could be
"RunBeginEnd"

Am I right ?

I hope to get some opinions.

Thanks in advance.


SCJP6 with score 90%. I am conscious of my ignorance and ready to learn from everyone.
Prithvi Sehgal
Ranch Hand

Joined: Oct 13, 2009
Posts: 774
Hello,

I guess the output is correct. First already the main thread is started and then we start the thread A, till it takes its turn
Begin will be printed and then a will take the turn and RUN will be printed. Then we say


Main joins a thread and then END is printed and the thread completes. Output is

BeginRunEnd

Rest i am still a bit confused, let others comment.

Best Regards,

Prithvi,
My Blog, Follow me on Twitter,Scjp Tips, When you score low in mocks, Generics,Scjp Notes, JavaStudyGroup
Nick Puketza
Greenhorn

Joined: Apr 01, 2010
Posts: 16
Hi Emanuele,

I agree with you that "RunBeginEnd" is a possible output.
After the "a" thread is started, it can be selected to
run right away, and so it could start executing the
run() method before the main thread prints "Begin."

I agree with you again that "BeginRunEnd" is also a
possible output.

In a way, the question is tricky because "RunBeginEnd"
is not listed as a possible result.

- Nick
Prithvi Sehgal
Ranch Hand

Joined: Oct 13, 2009
Posts: 774
Yes nick is correct. I just ran in debug mode and i got the answer as

RunBeginEnd.

I guess the question should be amended as, pick up all the correct answers and these two choices should be
selected.

Best Regards,
Emanuele Ghe
Ranch Hand

Joined: Feb 04, 2009
Posts: 111
Thanks all.
Rufat Piriyev
Ranch Hand

Joined: Mar 15, 2010
Posts: 31
IMHO


1.
a.start();
go to runnable
main thread execute
Begin
a.join() work go to a thread and go to running state
Run
a is dead
go to main thread
End

2.
a.start();
a going to running state
run is execute
Run
return to main thread
Begin
End

Be ready . Thread questions are very toughly

Prithvi Sehgal
Ranch Hand

Joined: Oct 13, 2009
Posts: 774
Rufat Piriyev wrote:
IMHO


Please UseRealWords

Thanks,
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Help in a question about Threads
 
Similar Threads
Threads Question
Concurrency question
Thread question from www.scjptest.com