aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread confusion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Thread confusion" Watch "Thread confusion" New topic
Author

Thread confusion

Francis Palattao
Ranch Hand

Joined: Sep 22, 2004
Posts: 91


This is a question from Dan C. Exam 20 Mock Test.

Does the code mean there are 10 threads (0-9) synchronized with their own private Object and waiting. While the main thread tries to synchronize on (this) which is itself (a bit confused which object this is holding on to)and then calls notifyAll.

Does it mean then that all 10 threads will be released and then print their name. Since they dont share a common private object all of them will then print their name.

Answer given was Nothing printed and Some or All of the threads will be printed.

My reasoning of nothing printed because main thread finishes and 10 threads are daemon threads. Right? But if they werent daemon all will be printed right? Also what if the private object they have is static. Does that mean only 1 thread will print their name?


SCJP 1.4 <br />SCWCD 1.4 (preparing)<br />SCBCD 1.3 (coming soon)
Anand Ko
Ranch Hand

Joined: Dec 03, 2003
Posts: 79
Does the code mean there are 10 threads (0-9) synchronized with their own private Object and waiting. While the main thread tries to synchronize on (this) which is itself (a bit confused which object this is holding on to)and then calls notifyAll.

- There is 10 threads synchronized on only 1 object and waiting. Main will only try to notify threads which are in wait state if the threads get chance to execute.


Does it mean then that all 10 threads will be released and then print their name. Since they dont share a common private object all of them will then print their name.

- they share the object

Answer given was Nothing printed and Some or All of the threads will be printed.

My reasoning of nothing printed because main thread finishes and 10 threads are daemon threads. Right? But if they werent daemon all will be printed right? Also what if the private object they have is static. Does that mean only 1 thread will print their name?

- Nothing printed : Main will finish creating 10 threads and will finish the notifyAll also. Then all the threads will go in wait state and will be in that state only
- If some : If some of threads manage to enter the wait state before main calls NotifyAll. Those threads will print and rest will be in wait state.


Anand<br />SCJP 1.4, SCWCD 1.4, SCEA 5.0(1/3)
Francis Palattao
Ranch Hand

Joined: Sep 22, 2004
Posts: 91
Thanks now its clear.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread confusion
 
Similar Threads
Working of notifyAll()
Thread Dan Question
Order of execution for wait and notify/notifyall
Thread
Danchisholm question -threads