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

后端 未结 5 588
清酒与你
清酒与你 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:33

    Thread.join(milis) does not kill the thread. It just waits for the thread to end.

    Java threading is cooperative: you can not stop or gracefully kill a thread without it's cooperation. One way to do it is to have an atomic flag (boolean field) that thread is checking and exiting if set.

提交回复
热议问题