java multithreading: interrupt () interrupts the thread, and the thread is stopped elegant principle

MyThread.class

package cn.yilong.edcsapiservice.thread;

public class MyThread extends Thread {
    @Override
    public void run() {
        System.out.println("开始睡觉");
        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("结束睡觉");

        synchronized (MyThread.class){
            for (int i=0; i<100; i++){
                System.out.print(i+",");
            }
            System.out.println();
            String name = Thread.currentThread().getName();
            System.out.println(name + ": " + this.isInterrupted());
            System.out.println("test--------------------");
            System.out.println();
        }

    }
}

 

Run.class

cn.yilong.edcsapiservice.thread Package; 

public  class Run2 { 

    public  static  void main (String [] args) { 
        the Thread CT = Thread.currentThread (); 
        ct.setName ( " mainThread " ); 

        the try { 
            the MyThread T = new new the MyThread ( ); 
            t.setName ( " myThread " ); 
            t.start (); 

            t.interrupt ();   // If the thread t has ended, and then transferred interrupt method, without any treatment 

            . the synchronized (MyThread class  ) {
                String name = Thread.currentThread () getName ();. 
                System. OUT .println (name + " : " + t.interrupted ()); // t thread calls though, but the bottom is not really call t, but the current thread main method, performing the actual operation is Thread.currentThread () isInterrupted () i.e. mainThread thread. 
                the System. OUT .println (t.getName () + " : " + t.isInterrupted ()); 
                the System. OUT .println ( " main ----------------------------- " ); 
                . the System OUT .println (); 
            } 


        } the catch (Exception E) { 
            the System.out.println("main catch");
            e.printStackTrace();
        }
    }
}

 

 




mainThread: false
myThread: true
main-----------------------------

 
 

Began to sleep
java.lang.InterruptedException: SLEEP interrupted
AT java.lang.Thread.sleep (Native Method,)
AT cn.yilong.edcsapiservice.thread.MyThread.run (MyThread.java:8)
ending sleep
0,1,2,3 , 4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28 , 29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53 , 54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78 , 79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,
myThread: false
the Test --- -----------------

 



interrupt () method

interrupt () method is essentially a call to the thread to make a mark, the mark default initial value is: false , became after the call: to true ,   
. So, do not stop and mark this thread and discarded stop () method is the immediate stop to this thread this is the fundamental difference between 

the thread object to call interrupt () method, we would like to interrupt this thread, people need to write logic interrupt,   
usually there are two cases (actually there are other, have to explain the source code comments): 
1 thread status: running , in which case if t thread calls interrupt () method, then, t thread will immediately identify the cook: false becomes true, just normal (of course, if t before thread calls interrupt () execution is over, 
then the tag will not change, still when false) t if the thread is still running, still continue execution, is marked will not be affected (the focus) , then we run the thread execution method t () by calling interrupted () or
to perform a manual end run () method of isInterrupted (). under long-winded: (
. 1 ) t.interrupt () after execution, marking: false to true change, run () method t.interrupted () and t .isInterrupted () will be acquired this flag is false, or to true. ( 2) T.interrupted () method attention to two things:
1. refer to the current thread calls, that is, if the mian () method, executed t.interrupted () method, the actual implementation is not t thread, a main thread (NB)
2.t.interrupted () method after execution returns a result of true or false, and it also performs operations further step: the results labeled t thread reset to default initial values: false. so, do not call it a second time. (special attention)
(3) t.isInterrupted () method refers to the t thread interrupt flag: true, or false, it only returns results, will not do other operations.

2. thread status: obstruction , such as calling the sleep (), or wait (), or join () blocking method, this time if t thread calls interrupt () method, then, t thread will immediately identify the cook: false change is true, at the same time,
blocking the t own thread at this time to monitor the interrupt flag is set to true, then dished out InterruptedException anomaly, while the results of the thread token t is reset to the initial default value: false (emphasis) .
Subsequently, we run in the thread execution method t () by try - catch catch the exception, however After the end of manual processing run () method.

 

Guess you like

Origin www.cnblogs.com/smileblogs/p/11563554.html