• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

KB Chapter 9 Question 9

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


According to the book, there should be a 10 second delay between "X" and "Y". Is this an error in the book? Both X and Y prints without delay here.
 
Sheriff
Posts: 9674
42
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
Actually this is an internal implementation detail of Thread class. When the run method of Thread class completes, any threads waiting on the thread object are notified automatically. But you cannot reply on this behavior, this is an implementation detail. So for the SCJP exam you can say that there will be 10 seconds delay between X and Y...
 
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@ Ankit, i think we can say that
" there would be a maximum of 10 seconds delay because thread is waiting and not sleeping "

When t.wait(10000) is encountered , t has already got finished, so the main thread would not wait and is immediately shifted to runnable. There is no reason for it to wait 10 seconds.

and in the above case if there are no other Threads running , then there would never a delay !!!
 
Grant Olson
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Got it! I misunderstood the use of the wait method a bit. Thanks for the help
 
Ankit Garg
Sheriff
Posts: 9674
42
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

When t.wait(10000) is encountered , t has already got finished, so the main thread would not wait and is immediately shifted to runnable. There is no reason for it to wait 10 seconds.


Let me clear this again, when you call wait on a thread object, your wait call will be automatically notified when the run method of the Thread completes (this will not happen in case you subclass the Thread class and override the run method). This is not given in the JLS, so you should not rely on this behavior. The program can behave differently in case the run method of Thread class completes before the wait method is called, in that case the wait method will wait for 10 seconds...
 
What are you doing in my house? Get 'em tiny ad!
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic