Thread.sleep(10000)


J

Jerry

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.
 
Ad

Advertisements

C

Chris Head

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
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-----
 
Ad

Advertisements

J

John Currier

Jerry said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top