• 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
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Doubt on a thread question from K&B OCP Practice Exams

 
Ranch Hand
Posts: 37
Eclipse IDE Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Below is a question from K&B's OCP practice exams book:


Which are true? (Choose all that apply.)
A. Compilation fails.
B. An exception is thrown at runtime.
C. Bang will execute for a second or two.
D. Bang will execute for at least 10 minutes.
E. Thread t1 will almost certainly be the last thread to finish.
F. Thread t1 will almost certainly be the first thread to finish.
G. It’s difficult to predict which thread will be the last to finish.

Below is answer and explanation from the book:
C and G are correct. The sleep() method’s argument is in milliseconds. E is incorrect
because sleep will be called once on each of the three threads, not on Thread t1 three
times. Remember, the sleep() method is static and operates on whichever thread is
the currently running thread.

But I don't know why C is correct. Could somebody explain it to me?
Thanks.
 
Ranch Hand
Posts: 59
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeffrey, could you please why do you think C can't be an answer ?
 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It depends on how you interpret the answer C. Bang will execute for a second or two. If your interpretation is that there is a possibility that all three threads will successfully execute in one second (1000ms), then C is wrong. If your interpretation is that there is a possibility that all three threads will successfully execute somewhere between one second and two seconds, then C is correct.
 
Ranch Hand
Posts: 1183
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By adding some time print statements, we can see that these threads run at about 672 msc, which makes sense as each runs for the 600 msc of pause + a minor time of running. So this statement of one/two seconds is vague but probably correct as if we round 672 msc we end up with a second.




Regards,
Dan
 
Jeffrey Tian
Ranch Hand
Posts: 37
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dan Drillich wrote:By adding some time print statements, we can see that these threads run at about 672 msc, which makes sense as each runs for the 600 msc of pause + a minor time of running. So this statement of one/two seconds is vague but probably correct as if we round 672 msc we end up with a second.




Regards,
Dan



Thank you, Dan.
 
Jeffrey Tian
Ranch Hand
Posts: 37
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

nitin sethi wrote:Jeffrey, could you please why do you think C can't be an answer ?



My thought is similar to Dan's. It should execute for less than 1 second because these 3 threads don't need to compete for a lock and they can pause simultaneously.
 
Dan Drillich
Ranch Hand
Posts: 1183
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Absolutely, they practically sleep for 600 concurrently.

Regards,
Dan
 
Dan Drillich
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe it's not that simple. It's not clear to me what the relation between the thread and the core-cpu is during these 600 msc. Is the thread bound to the cpu during this time-frame?

Regards,
Dan
 
Ranch Hand
Posts: 101
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We can say that the result will be the same if we replace t1.sleep() in run()method with either of t2.sleep(), t3.sleep() or sleep(), don't we?
 
Ranch Hand
Posts: 924
1
Netbeans IDE Fedora Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Joey Sanchez wrote:We can say that the result will be the same if we replace t1.sleep() in run()method with either of t2.sleep(), t3.sleep() or sleep(), don't we?



yes the result will be same. as said in earlier posts sleep() method is static method of Thread class and each of t1, t2,t3 is of type Thread so each of t1.sleep(), t2.sleep(),t3.sleep() will be replaced internally by Thread.sleep() which means the current running thread will sleep.
 
Wait for it ... wait .... wait .... NOW! Pafiffle! A perfect tiny ad!
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic