Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Threads: sleep() !  RSS feed

 
Prathamesh Gaddam
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Towards the following code I'd expected papered output as:
"StartMain Start1 Start2 Error End2 EndMain End1"

However I'm getting the output as:
"compile-single:
StartMain Startl MainEnd End1 Start2 Error
End2
debug-single:
BUILD SUCCESSFUL (total time: 20 seconds)

"
Please clarify and assist to rectify


[ June 27, 2008: Message edited by: Prathamesh Gaddam ]
 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please explain why you are expecting the output to be different from what it is? There must be a fault in your reasoning. But if you don't show us what your reasoning is, how can we help you?
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hint: What does the synchronized (resource) does to all the timing? Are there any delays, elsewhere, besides calls to the delay method.

Henry
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66204
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please be sure to take the time to compose descriptive subjects for your posts; read this for more information.

Using a title of "Thread" in a forum completely dedicated to questions on threads isn't very helpful.

You can go back and change your post to add a more meaningful subject by clicking the .
[ June 27, 2008: Message edited by: Bear Bibeault ]
 
Prathamesh Gaddam
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Norm,

Because of the delay/sleep duration. Main thread delay's 1 sec between invoking thread1 and thread2. Thread1 has a delay of 6 sec while thread 2 has delay of 2 sec. Therefore as per the delays processed by thread are in ascending order main thread, thread2 and thread1. So I assumed the output as

StartMain Start1 Start2 Error End2 EndMain End1


I hope this may elaborate my concerns. If further clarification is needed please reply. I will do my best!
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the javadoc of Thread.sleep: "The thread does not lose ownership of any monitors."
 
Prathamesh Gaddam
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the guidance. I'd referred javadoc for Thread.sleep(). However, fundu is not clear to me. Please clarify.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thread 2 can't enter its synchronized block before thread 1 has finished its own. A call to Thread.sleep doesn't change that.
 
Prathamesh Gaddam
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understood. Thank you and thank you Ilja, for the reply with simplicity and completeness, respectively.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!