0

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

2
  • 3
    The easiest and most exciting way to solve this is to test it out yourself with code you've created. What happens when you do this? Commented Mar 13, 2013 at 1:31
  • 1
    Another way is to look at the Java API source code files that ship with the JDK, in the src.zip file. Commented Mar 13, 2013 at 1:42

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.