• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Thread question from Sun

 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have purchased the scjp exam questions from sun and found this one:

Given:



Which two are true? (Choose two.)

A The program never completes.
B The program runs to completion.
C The output can be 6 7 8 6.
D The output can be 6 7 8 10.
E The output can be 6 7 8 6 7 10.
F The output can be 6 7 10 7 10 6.

The answer is A and D. I understand why it is A, but not sure on how it could be D. Could someone please shine some light here for me?

Thanks,

Cory
 
Cory Max
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is the answer they attach to the question:

Options A and D are correct. Because there are two runnables, neither one's state ever equals 3, so the else block is never executed and the threads wait forever. C is incorrect because four different threads are started and each must have a unique Id.
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The first thread enters the synchronized "run" method. The state field is 0 after the initialization, so the thread enters the if block and prints its id. The second thread cannot yet enter the run method, as the method is synchronized on the w1 object, which is already locked by the first thread.

But then the first thread frees the lock by calling the wait method on the w1 object (which is referred with a "this" link), and the second thread enters the run method and also prints its id. Then the second thread also frees the lock calling the wait method.

But as the wait methods are run without parameters, this means that both threads will wait until some other thread calls notify on the w1 object, which will never happen, because the third and second thread run on another object, w2, independently, and stuck in the same situation (state field is not static, so it's also 0 at the initialization of the w2 object), managing only to print their ids just once. So we have 4 different ids printed on the screen, which corresponds to the D answer.
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mark the state field as static and work on the code . Its Interesting
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not sure if your question has already been answered to your satisfaction but essentially the reason that D is correct is because since each time a new thread was created (4 times) it got a unique ID. And since each thread only prints the "getId()" method once each thread ID has to be unique. Option D is the only one with 4 each thread ID being unique so that has to be it.

Hope that helps
[ December 16, 2007: Message edited by: Panseer Kaur ]
 
Cory Max
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wow... for some reason I did not see the first sop(getId). Sorry for the silly question and thanks for the responses.
 
Cory Max
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wow... for some reason I did not see the first sop(getId). Sorry for the silly question and thanks for the responses.
 
Cory Max
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wow... for some reason I did not see the first sop(getId). Sorry for the silly question and thanks for the responses.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic