Access Thread from inside Runnable

alex picture alex · Aug 13, 2012 · Viewed 8.6k times · Source

How can I access Thread object that is executing given Runnable? What I'd like to do is to call sleep() from within run() method.

Answer

assylias picture assylias · Aug 13, 2012

If you need to sleep within a Runnable, you can simply call Thread.sleep(); inside the run method.

To access the thread that is running the Runnable, you can call Thread.currentThread(), but that is not necessary to call the sleep method, which is static.