Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread confusion

 
Fran Kindred
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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?
 
Anand Ko
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Fran Kindred
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks now its clear.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic