Java Basics: Threading method Interrupt and sleep

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.