How do I get a thread to pause?
Created May 4, 2012
John Zukowski The static sleep() method of the Thread class will let your thread sleep for a set number of milliseconds (or nanoseconds). When the thread wakes up, it will be scheduled to execute in the future. It may not start executing immediately after the timeout.
try { Thread.sleep(3000); // 3 seconds } catch (InterruptedException e) { System.err.prinlnt("You interrupted me"); }[
You can have another thread wake up the sleeping thread prematurely by calling t.interrupt() (where "t" is a pointer to the thread object).
Note that since Thread.sleep is a static method, the following code will not do what you expect it to:
Thread t = new MyThread(); t.start(); try { t.sleep(3000); } catch (InterruptedException e) { System.err.prinlnt("You interrupted me"); }It will pause the current thread, meaning the thread executing the shown code, not the child thread (t/MyThread). -Alex]