JAvaスレッドの安全起動停止、フラグビット通過


JAvaスレッドの安全起動停止は、フラグビットを介してjavaスレッドのstop()メソッドで、使用を推奨せず、期限切れのAPIのために、どのように安全にスレッドを停止、終了しますか?フラグビットを使用することで、次のことができます.
結果パラメータを定義して、スレッドの起動と停止、終了を制御します.
private boolean start = false;  //    
private boolean stop = false;   //    
private boolean quit = false;   //      

次にRunnableメソッドの最上位Taskクラスを実装する
import java.util.concurrent.TimeUnit;

public class TaskRun implements Runnable {
    private Object o = new Object();
    private boolean start = false;  //    
    private boolean stop = false;   //    
    private boolean quit = false;   //    

    @Override
    public void run() {
        //               
        this.start = true;
        //        false   
        this.stop = false;
        this.quit = false;
        while (start) {
            try {
                System.err.println("run ---------------------");
                TimeUnit.SECONDS.sleep(5);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            //      true ,  
            if (quit) {
                this.start = false;
                break;
            }
            //       true      
            if (stop) {
                System.err.println("stop run---------------------");
                synchronized (this) {
                    //   start   ,        
                    try {
                        this.start = false;
                        this.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.err.println("recovery run---------------------");
                //     ,      
                this.start = true;
                this.stop = false;
            }

        }
    }

    public Object getO() {
        return o;
    }

    public void setO(Object o) {
        this.o = o;
    }

    public void stop() {
        this.stop = true;
    }

    public void quit() {
        this.quit = true;
    }

    public boolean restart() {
        synchronized (this) {

            this.notify();
        }
        return start;
    }

    public boolean isStart() {
        return start;
    }

    public boolean isStop() {
        return stop && !start;
    }

    public boolean isQuit() {
        return quit;
    }

}

次にテストを作成します
    TaskRun taskRun = null;
    Thread thread = null;

    @RequestMapping(value = { "run/start" })
    @ResponseBody
    public Object start() {
        if (taskRun == null || !taskRun.isStart()) {
            taskRun = new TaskRun();
            System.out.println("Start...");
            thread = new Thread(taskRun, "AAAAAAAAAAAAAAAAAAAAAAAA");
            thread.start();
            while (!taskRun.isStart() && !thread.isAlive()) {
                System.out.print("...");
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            return "ok";
        } else {
            return "    ";
        }
    }

    @RequestMapping(value = { "run/restart" })
    @ResponseBody
    public String restart() {
        System.out.println("Start...");
        if (!taskRun.isStart()) {
            taskRun.restart();
            if(!thread.isAlive()){
                System.out.println("new start...");
                thread = new Thread(taskRun);
                thread.start();
            }
            while (!taskRun.isStart()) {
                System.out.print("...");
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            return "ok";
        } else {
            return "    ";
        }
    }

    @RequestMapping(value = { "run/stop" })
    @ResponseBody
    public String stop() {
        taskRun.stop();
        System.out.println("stop");
        while (!taskRun.isStop()) {
            System.out.print("...");
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        return "ok";
    }

    @RequestMapping(value = { "run/quit" })
    @ResponseBody
    public String quit() {
        taskRun.quit();
        System.out.println("stop");
        while (taskRun.isQuit() && thread.isAlive()) {
            System.out.print("...");
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        taskRun = null;
        thread = null;
        return "ok";
    }