Timeout a task with Java's SwingWorker

前端 未结 4 1306
不知归路
不知归路 2021-01-02 22:52

I am trying to implement a SwingWorker class within my application. Is there a way to set a length of time that after which, the SwingWorker \"times out\"? I

4条回答
  •  执笔经年
    2021-01-02 23:26

    The short answer is "it's hard", depending on what your requirements are. I highly recommend reading Java Concurrency In Practice.

    The basic thing you could do would be to (a) make sure your SwingWorker's Runnable is interrupt-friendly, and (b) set a Timer (or use the blocking get() call Brian mentioned) to cancel your Future.

提交回复
热议问题