File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Blocking a thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Blocking a thread" Watch "Blocking a thread" New topic
Author

Blocking a thread

jibs parap
Ranch Hand

Joined: Dec 07, 2004
Posts: 134
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

Joined: Jun 05, 2006
Posts: 117
only the current executing thread (Thread.CurrentThread) will sleep
Alina Petra
Greenhorn

Joined: Oct 17, 2006
Posts: 26
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

Joined: Aug 15, 2006
Posts: 165
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)".


Matt
Inquisition: open-source mock exam simulator for SCJP and SCWCD
jibs parap
Ranch Hand

Joined: Dec 07, 2004
Posts: 134
Many thanks guys.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Blocking a thread
 
Similar Threads
Question on sleep() method from dan's
Thread behavior with synchronized method
Deadlocks in Threads?
Clarify Use of the join method
Concurrency Question with Thread.sleep(5000) ?