42. Thread stop

Generally, when we use multi-threading, we usually use loops. After all, if there is only one statement, there is no need to use multi-threading.

So how to stop a thread?

There are two types of thread stop, either the task is completed, or it is forced to stop

Thread stop:

    1. We can define a boolean type variable combined with the notity method to control the stop of the thread (notity is used to prevent the thread to be stopped from waiting)

    2. You can also use the interrupt method (forced stop will cause the wait method to throw an exception). As for the start method, it is not recommended to use it.

Here is an example of two ways to stop a thread:

  An example of the way:

public class Demo10 implements Runnable{
    boolean flas = true;
    @Override
    public void run() {
        while(flas) {
            synchronized (this) {
                try {
                    this.wait();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            System.out.println(Thread.currentThread().getName());
        }
        
    }
    public static void main(String[] args) {
        Demo10 d = new Demo10();
        Thread thread = new Thread(d,"狗娃");
        thread.start();
        for ( int i = 0; i < 100; i++ ) {
             // If i=80 of the main thread, stop the dog thread 
            if (i==20 ) {
                d.flas = false;
                synchronized (d) {
                    d.notify();
                }
            }
            System.out.println(Thread.currentThread().getName()+i);
        }
    }
}

 

Method 2: Example 2

public class Demo10 implements Runnable{
    boolean flas = true;
    @Override
    public void run() {
        while(flas) {
            synchronized (this) {
                try {
                    this.wait();
                } catch (InterruptedException e) {
                    System.out.println( "Throw exception" );
                }
            }
            System.out.println(Thread.currentThread().getName());
        }
        
    }
    public static void main(String[] args) {
        Demo10 d = new Demo10();
        Thread thread = new Thread(d,"狗娃");
        thread.start();
        for ( int i = 0; i < 100; i++ ) {
             // If i=80 of the main thread, stop the dog thread 
            if (i==20 ) {
                d.flas = false;
                thread.interrupt(); // Forcibly stop the specified thread 
            }
            System.out.println(Thread.currentThread().getName()+i);
        }
    }
}

 

 Note: These two examples will appear after 20, and there will be a statement output by the dog thread

  It should be that the dog thread has just judged it to be true and has not yet rushed to execute the output statement. The cpu is forced by the main thread, which will cause it to be executed again later (but the thread is really stopped)

 It may also be caused by wait (this may also happen if it is removed, after all, we do not use locks)

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325075787&siteId=291194637