File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Blocking a thread

 
jibs parap
Ranch Hand
Posts: 134
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sleep() is a static method but you can invoke it on an instance.

Suppose, you have 2 threads of execution, t1 and t2. In your t1's run method, if you say t2.sleep(1000), will it cause t2 to sleep?

'One thread can't tell another to block'. Can you explain this in the light of the above example please.

Thanks
 
joshua antony
Ranch Hand
Posts: 117
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
only the current executing thread (Thread.CurrentThread) will sleep
 
Alina Petra
Greenhorn
Posts: 26
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As you said, sleep() is a static method. It causes the current thread to sleep for a period of time. It always refers to the current thread. It doesn't matter if you invoke it using the class name or an instance of the class, it will always cause the current thread to sleep.
 
Matt Russell
Ranch Hand
Posts: 165
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by jibs parap:
sleep() is a static method but you can invoke it on an instance.

Suppose, you have 2 threads of execution, t1 and t2. In your t1's run method, if you say t2.sleep(1000), will it cause t2 to sleep?

No -- calling sleep() causes the currently executing thread to sleep. "t2.sleep(1000)" has the same effect as "Thread.sleep(1000)".
 
jibs parap
Ranch Hand
Posts: 134
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Many thanks guys.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic