Swingworker Timeout

后端 未结 1 963
迷失自我
迷失自我 2021-01-19 09:26

I am using a SwingWorker to read data over a TCP connection and display when it comes back.

new SwingWorker() {
  @Override
  publi         


        
相关标签:
1条回答
  • 2021-01-19 10:04

    Yes, the solution you linked to is a reasonable and easy solution ("best" is so subjective :) You could leverage SwingWorker#get, that is part of the Future interface:

    SwingWorker<EnvInfoProto, Void> worker = new SwingWorker<EnvInfoProto, Void>() {
        ...
    };
    worker.execute();
    worker.get(15, TimeUnit.SECONDS); 
    //will block 15 seconds at most, then throw TimeoutException
    

    Of course you could come up with different ways to reach your goal, but I'd bet there is more code involved than in this solution, so I'd give it a try.

    0 讨论(0)
提交回复
热议问题