stop a thread in java after a given time - doesn't work

后端 未结 5 542
清酒与你
清酒与你 2021-01-25 04:06

I have a complex function (optimisation) that can potentially enter in a loop or just to take too much time, and the time allowed is set by the user.

Therefore I am tryi

5条回答
  •  执笔经年
    2021-01-25 04:57

    The join method will wait the current thread until the thread that is being joined on finishes. The join with milliseconds passed in as a parameter will wait for some amount of time, if the time elapses notify the waiting thread and return.

    What you can do, is after the join completes interrupt the thread you joined on. Of course this requires your thread to be responsive to thread interruption.

提交回复
热议问题