• 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

Joining a sleeping thread?

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I came across a question where a Thread t is defined and started before a Thread s.
- Thread s calls t.join() (so the first Thread) as its first statement, afterwards it alternates between sleeping and printing some sequence (for loop).

- Thread t goes to sleep as his first statement, then prints out something and then alternates between sleeping and printing for some time (for loop)

To my knowledge, in this situation there's no guarantee that t will be running when s invokes t.join(), as the start sequence seems not to guarantee an order of execution. In this case, would s start anyway?

Moreover, in the Bates/Sierra book, it is stated that sleep actually helps threads taking turns - i.e. the other one could jump in while the first one sleeps.

In spite of all this, the solution to this question (LearnKey master exam) says that the order of printouts is ordered: first the ones from thread t, then the ones from thread s.
Am I missing something here?
 
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're making it way too complicated. When the thread s calls "t.join()", that means it waits for the thread t to finish. It doesn't matter what the thread t is doing -- sleeping, waiting, working, whatever -- thread s just doesn't do anything at all until thread t finishes.
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes and sleep gives other threads a chance to run when the other thread is runnable. in case of s, it is blocked by the call to join.
I think I am correct...I might have wrongly used the word blocked for thread s....
 
reply
    Bookmark Topic Watch Topic
  • New Topic