简体   繁体   中英

Access Thread from inside Runnable

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.

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.

you can always get the current Thread that your code is executing within by calling the static method Thread.currentThread() .

But in your case, you don't need the thread itself because sleeping is controlled with the following static method:

Thread.sleep(1000L);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM