简体   繁体   中英

java timer and thread.sleep

let's say I have a thread running, and inside there is a timer.schedule(task, 5000). let's now assume that timer started running, and somewhere in the thread, there is thread.sleep(). While the thread is still sleeping, the 5000 milisecond passes. Does the timer's task still run while the thread it is in is sleeping?

Thanks

Thread.sleep() cannot be somewhere in the thread , it can be in the task code, so the task execution will block for 5000 ms and then run to completion.

Note that java.util.Timer is a single-threaded scheduler, and if a task execution is blocked Timer will not be able to execute other scheduled tasks. See API

Corresponding to each Timer object is a single background thread that is used to execute all of the timer's tasks, sequentially. Timer tasks should complete quickly . If a timer task takes excessive time to complete, it "hogs" the timer's task execution thread. This can, in turn, delay the execution of subsequent tasks, which may "bunch up" and execute in rapid succession when (and if) the offending task finally completes.

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