Thread.sleep(10000)  
Author Message
Jerry





PostPosted: 2005-8-1 12:38:00 Top

java-programmer, Thread.sleep(10000) Is that true Thread.sleep(10000) will make current thread to sleep for
10 seconds?

I cannot use Thread.currentThread().sleep(10000) since sleep is a
static method of Thread. Right? Thread.currentThread() will return a
reference to the currently executing thread object.

 
Chris Head





PostPosted: 2005-8-1 13:58:00 Top

java-programmer >> Thread.sleep(10000) -----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Jerry wrote:
> Is that true Thread.sleep(10000) will make current thread to sleep for
> 10 seconds?

Approximately true. The thread will sleep for 10 seconds then become
scheduled. It will probably run shortly thereafter. The only exception
is that if another thread uses theThread.interrupt(), the sleep() call
will return early and throw InterruptedException.

This is in the JavaDocs. See this location:

http://java.sun.com/j2se/1.5.0/docs/api/

>
> I cannot use Thread.currentThread().sleep(10000) since sleep is a
> static method of Thread. Right? Thread.currentThread() will return a
> reference to the currently executing thread object.
>

You can actually enter the line of code you indicated, but it's highly
discouraged, because it means exactly the same as:

Thread.sleep(10000);

It is true that Thread.currentThread() returns a Thread object referring
to the currently executing thread.

Chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (MingW32)

iD8DBQFC7bnR6ZGQ8LKA8nwRAoDNAKCVXZVyYyRsZe4aZ/BDUjAb5dDBegCfaf4e
v7AimQzQ8h1yOSqIyOnG/4o=
=F+6x
-----END PGP SIGNATURE-----
 
John Currier





PostPosted: 2005-8-3 2:30:00 Top

java-programmer >> Thread.sleep(10000) Jerry wrote:
> Is that true Thread.sleep(10000) will make current thread to sleep for
> 10 seconds?
>
> I cannot use Thread.currentThread().sleep(10000) since sleep is a
> static method of Thread. Right? Thread.currentThread() will return a
> reference to the currently executing thread object.

What Chris said plus: you are correct in that Thread.sleep() is static
and as such it only impacts the current thread.

Code like otherThread.sleep(1000) logically appears to cause
otherThread to sleep for about a second, but the current thread is the
one that actually sleeps.

John
http://schemaspy.sourceforge.net