小编Den*_*er9的帖子

定时循环不终止

运行此代码,我希望它将测试变量增加5秒然后完成.

import java.util.Timer;
import java.util.TimerTask;

public class Test {
    private static boolean running;

    public static void main( String[] args ) {
        long time = 5 * 1000;       // converts time to milliseconds
        long test = Long.MIN_VALUE;
        running = true;

        // Uses an anonymous class to set the running variable to false
        Timer timer = new Timer(); 
        timer.schedule( new TimerTask() { 
            @Override
            public void run() { running = false; }
        }, time );

        while( running ) {
            test++;
        }

        timer.cancel();
        System.out.println( …
Run Code Online (Sandbox Code Playgroud)

java timer while-loop timertask

5
推荐指数
1
解决办法
91
查看次数

标签 统计

java ×1

timer ×1

timertask ×1

while-loop ×1