Understanding java-17.2 thread interrupts from scratch (interrupt)

Source: Internet
Author: User

In this chapter we will discuss thread break (interrupt).

1. What is thread interruption (interrupt)?

It is when we run multithreading that we attach a broken token to the thread, but we do not require the thread to terminate.

2. Example:

Examples of interrupts:

Package Com.ray.ch17;public class Test2 {public static void main (string[] args) {PrintA PrintA = new PrintA (); Thread Threada = new Thread (PrintA); Threada.start ();}} Class PrintA implements Runnable {private static int i = 0; @Overridepublic void Run () {while (! Thread.CurrentThread (). isinterrupted ()) {System.out.println ("PrintA"), if (i = = 2) {Thread.CurrentThread (). Interrupt ();} i++;}}}


Output:

PrintA
PrintA
PrintA

Examples of non-disruptive:

Package Com.ray.ch17;public class Test2 {public static void main (string[] args) {PRINTB printb = new Printb (); Thread threadb = new Thread (PRINTB); Threadb.start ();}} Class Printb implements Runnable {@Overridepublic void Run () {for (int i = 0; i < 5; i++) {System.out.println ("PRINTB") ; Thread.CurrentThread (). interrupt ();}}}


Output:

Printb
Printb
Printb
Printb
Printb

As we can see from the two examples above, interrupt just posts an interrupt mark and does not force an interrupt.

3.interrupt clash with Sleep

Because when used with sleep after interrupt, sleep will remove the interrupt mark

Conflict code, the following code will be output indefinitely:

Package Com.ray.ch17;public class Test2 {public static void main (string[] args) {PrintA PrintA = new PrintA (); Thread Threada = new Thread (PrintA); Threada.start ();}} Class PrintA implements Runnable {private static int i = 0; @Overridepublic void Run () {while (! Thread.CurrentThread (). isinterrupted ()) {System.out.println ("PrintA"), if (i = = 2) {Thread.CurrentThread (). Interrupt (); try {thread.currentthread (). Sleep ()} catch (Interruptedexception e) {//TODO auto-generated catch Blocke.printstacktrace ();}} i++;}}}


Summary: This chapter focuses on threading interrupts (interrupt).

This chapter is here, thank you.

-----------------------------------

Directory

Understanding java-17.2 thread interrupts from scratch (interrupt)

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: info-contact@alibabacloud.com 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.