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

Thread confusion

 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks now its clear.
reply
    Bookmark Topic Watch Topic
  • New Topic