How can I wrap a method so that I can kill its execution if it exceeds a specified timeout?

后端 未结 7 1764
陌清茗
陌清茗 2020-11-30 08:55

I have a method that I would like to call. However, I\'m looking for a clean, simple way to kill it or force it to return if it is taking too long to execute.

I\'m

7条回答
  •  失恋的感觉
    2020-11-30 09:30

    I'm assuming the use of multiple threads in the following statements.

    I've done some reading in this area and most authors say that it's a bad idea to kill another thread.

    If the function that you want to kill can be designed to periodically check a variable or synchronization primitive, and then terminate cleanly if that variable or synchronization primitive is set, that would be pretty clean. Then some sort of monitor thread can sleep for a number of milliseconds and then set the variable or synchronization primitive.

提交回复
热议问题