A better way to run code for a period of time

前端 未结 4 652
遥遥无期
遥遥无期 2020-12-13 22:50

I need to run some code for a predefined length of time, when the time is up it needs to stop. Currently I am using a TimerTask to allow the code to execute for a set amount

4条回答
  •  半阙折子戏
    2020-12-13 22:57

    If by stopping you mean the program has to exit, the solution is to create a thread for your processing and mark it as daemon, start it and in the main thread sleep for the time required, then simply return from the main() method.

    Scratch that if by stopping, you mean just to stop the processing.

提交回复
热议问题