• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Sun's Assessment Exam: Concurrency #24

 
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I finished K&B and I am now re-reading it. Just to test my knowledge I tried my luck on the Sun's Assessment Exam and I got an embarrassing score of 27/42=64%

I still have 1.5 months to go before my exam and I guess from this time on I would be taking a lot of mocks and target at least 90% average as everybody suggests.

Anyway, as I have said in my subject, there is this one question in concurrency that really freaked me out. The choices for me seemed very impossible to obtain. Without using eclipse or running the code somewhere, I couldn't get the idea how the number printouts reach ..6 7 8.. as posted in the choices.

Also, what do you expect to print when you call this line,

System.out.print(" " + Thread.currentThread().getId());

It's supposed to print something like "Thread-0"... Isn't it? How come that string doesn't exist in any of the choices..

I hope it's OK to post the question here...



I hope you can help me analyze this code. Thank you very much in advance.

[ August 14, 2008: Message edited by: Denise Saulon ]
[ August 14, 2008: Message edited by: Denise Saulon ]
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I say, there's no correct choice.
at the line #8, the current thread releases the lock, then it enters the blocking/waiting state.
And the important thing here, there's no another live thread who acquire the lock. So there will be one thread's id printed.

Somebody correct me if i'm wrong.
 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
am preparing for scjp1.5, but i haven't been aware of what is "question in concurrency". is that under threads topic???
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
C and E should be the correct answers.

>> 6. if (state++ < 3) {
is a key line here, I think.
When new threads are created and started in line 19, 20, 21 and 22, each thread accesses this "state" variable and it gets incremented each time. First three threads will enter the "if" block and wait() forever. The fourth thread when started, since "state" has already incremented to 3, execution goes to "else" block.

Then any one of first three threads could be notified so possible outputs would be
6 7 8 6 7 8 <<== C
6 7 8 6 8 7
6 7 8 7 6 8
6 7 8 7 8 6
6 7 8 8 6 7 <<== E
6 7 8 8 7 6

Hope this helps.

Fei
 
Denise Advincula
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you so much for sharing this insight. However, what made me more confused than ever is that there is no line in the code that prints the state variable such as this:

..
System.out.println(state+" ");
..

Only the lines of Thread.currentThread().getID() get printed... That's why I'm not sure how the numbers in the choices appeared..
 
Denise Advincula
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by venkatesh badrinathan:
am preparing for scjp1.5, but i haven't been aware of what is "question in concurrency". is that under threads topic???



I think so.. in the Sun's Free Assessment Exam, the section is marked as

Concurrency
Page 4 of 7 (items: 19-24 of 42)
[ August 14, 2008: Message edited by: Denise Saulon ]
 
fei long
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are right, it doesn't print out "state" variable. Those 6 7 8 9 are thread ID (printed by "Thread.currentThread().getID()") and you don't need to know why they are not 11, 310 etc. (As you see, all options start as 6 7 8, so this number is not important)

The key thing you need to know is three threads execution go to wait(), and one thread go to notify()/notifyall() and this behavior is because all threads share the access to "state" variable (not concurrently though). And it also tests "synchronized" and "thread execution order is not guaranteed" concepts
 
Denise Advincula
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by fei long:
You are right, it doesn't print out "state" variable. Those 6 7 8 9 are thread ID (printed by "Thread.currentThread().getID()") and you don't need to know why they are not 11, 310 etc. (As you see, all options start as 6 7 8, so this number is not important)

The key thing you need to know is three threads execution go to wait(), and one thread go to notify()/notifyall() and this behavior is because all threads share the access to "state" variable (not concurrently though). And it also tests "synchronized" and "thread execution order is not guaranteed" concepts



Oh! This is a very sad question.. My mind was tricked to think that the state variable is going to be printed. And I didn't notice that the numbers are actually Thread IDs, as opposed to Thread NAMES that I was thinking before. Anyway, thanks again. I'm still trying to analyze your answer but at least I've got direction now.
 
fei long
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No worries. Exam question always tries to trick people.

One thing that might help you understand this (or any other complicated) code is to run it by yourself and see the result. Then make some changes or add some extra logging and observe the behavior. I found this helped me understand some of confusing concepts
 
Faber Siagian
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oops, apparently my understanding is wrong about this.
thanks guys for pointing out the right thing
 
It's just a flesh wound! Or a tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic