如何使用布尔标志停止正在运行的线程不起作用

时间:2017-09-17 07:50:08

标签: java multithreading

我有这段代码。当我尝试匹配if语句中的条件时,如果它满足,则running布尔值应设置为false。但它(Thread)仍然有效。任何人都可以告诉我我正在做的错误吗?

    public void timer() {

        new Thread() {
            @Override
            public void run() {
                int min = 1;
                int sec = 10;
                System.out.println("--- " + min + " Minutes and " + sec + " Seconds ---");
                boolean running = true;
                while (running) {
                    System.gc();
                    try {                        
                        for (int i = sec; i <= 60; i--) {
                            if (i >= 0) {
                                System.out.println(min + " Minutes and " + i + " Seconds"); 
                                Thread.sleep(1000);
                            } else {
                                i = 60;
                                min--;
                                if (min < 0) { //condition I'm checking
                                    System.out.println("OOOOOOOOOOOOOKKKKKKKKKKKKKKKKKKKKKKKKK"); 
                                    running=false; //while loop should be false and thread should not run anymore
                                }
                            }
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }.start();
    }

一切都很好。 (即使&#34; OKKKKKKKK ......&#34;也在打印),但running未设置为false

2 个答案:

答案 0 :(得分:0)

首先,您设置<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <select id="test"> <option value="">select</option> <option>1</option> <option>2</option> <option>3</option> <option>4</option> </select>(10)
然后你打印并等待i = min
然后你设置if (i >= 0)
并且i = 60;正在逐渐减少(i) 请注意,i--ii <= 60;循环,因为for循环会永远检查 因此,running将为false,但永远不会在while循环中检查

尝试增加i,无论你想做什么:)

答案 1 :(得分:0)

程序逻辑中的问题是你在for循环中设置运行变量false的值并且还重置i = 60的值 - 运行变量值设置为false但是controll永远不会返回while循环所以线程永远不会终止

检查此添加break语句执行技巧并控制返回while循环并且线程成功终止

       new Thread() {
            @Override
            public void run() {
                int min = 1;
                int sec = 10;
                System.out.println("--- " + min + " Minutes and " + sec + " Seconds ---");
                boolean running = true;
                while (running) {
                    System.gc();
                    try {                        
                        for (int i = sec; i <= 60; i--) {
                            if (i >= 0) {
                                System.out.println(min + " Minutes and " + i + " Seconds"); 
                                Thread.sleep(1000);
                            } else {
                                i = 60;
                                min--;
                                if (min < 0) { //condition I'm checking
                                    System.out.println("OOOOOOOOOOOOOKKKKKKKKKKKKKKKKKKKKKKKKK"); 
                                    running=false; //while loop should be false and thread should not run anymore
                                break;
                                }
                            }
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }