One: See demo directly
Output Result:
-------Tue Feb 19:57:02 CST----
-------Tue Feb 19:57:03 CST----
-------Tue Feb 19:57:04 CST----
-------Tue Feb 19:57:05 CST----
-------Tue Feb 19:57:06 CST----
-------Tue Feb 19:57:07 CST----
-------Tue Feb 19:57:08 CST----
-------Tue Feb 19:57:09 CST----
-------Tue Feb 19:57:10 CST----
-------Tue Feb 19:57:11 CST----
It
can be seen that, after 10 seconds, the thread terminates, Thread.Interrupt (), indicating that the thread was interrupted.
Second: Knowledge points
(1) thread.sleep (10000); The thread method is called on which thread, which is the one that represents it. Here is the main thread of the Mian
(2) The bottom-level implementation of the Sleep method:throws Interruptedexception Public
static native void sleep (long Millis) throws interruptedexception;
So when you call the Sleep method, you must capture its exception.
(3) interrupt () interrupt termination thread, very rude!
(4) sleep () dormant for some time, not terminate!
Java Basics: Threading method Interrupt and sleep