How to stop execution after a certain time in Java?

后端 未结 4 1459
攒了一身酷
攒了一身酷 2020-11-28 07:01

In the code, the variable timer would specify the duration after which to end the while loop, 60 sec for example.

   while(timer) {
    //run
    //terminate         


        
4条回答
  •  抹茶落季
    2020-11-28 07:13

    long start = System.currentTimeMillis();
    long end = start + 60*1000; // 60 seconds * 1000 ms/sec
    while (System.currentTimeMillis() < end)
    {
        // run
    }
    

提交回复
热议问题