当前位置:网站首页>Thread status and stop

Thread status and stop

2022-06-26 05:59:00 Mr.Rop

Thread state

 Thread state

Thread stop

public class TestStop implements Runnable {
    
    // 1. Set a flag bit 
    private boolean flag = true;
    @Override
    public void run() {
    
        int i = 0;
        while (flag){
    
            System.out.println("run....Thread"+i++);
        }
    }
    // 2. Set an open method to stop the thread , Conversion flag bit 
    public void stop(){
    
        this.flag = false;
    }
    public static void main(String[] args) {
    
        TestStop testStop = new TestStop();
        new Thread(testStop).start();

        for (int i = 0; i < 1000; i++) {
    
            System.out.println("main"+i);
            if (i == 900){
    
                testStop.stop();
                System.out.println(" It's time for the thread to stop ");
            }
        }
    }
}
原网站

版权声明
本文为[Mr.Rop]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202180503012158.html