• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help in a question about Threads

 
Emanuele Ghe
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Prithvi Sehgal
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,
 
Nick Puketza
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 774
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all.
 
Rufat Piriyev
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 774
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rufat Piriyev wrote:
IMHO


Please UseRealWords

Thanks,
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic