Java SwingWorker not terminating on task completion

前端 未结 2 1708
陌清茗
陌清茗 2021-01-23 09:07

Ok, so I\'ve been playing around with SwingWorker a bit and got some simplified code for updating a gui going, but I\'m having trouble figuring out how to get the thread to prop

2条回答
  •  情歌与酒
    2021-01-23 09:47

    I'm not sure what you're trying to achieve. Your example is working fine. Worker thread runs till the end. In case you want to wait till it ends to do something, you have to call method pbTask.get() somewhere in your code. Otherwise, it will just quietly completes without affecting any of your UI components.

    Consider the following change to your method to see how it behaves now. Note, that UI freezes because you make UI wait for the thread to complete, but output "DONE" appears in your log only when the WorkerThread completes.

    public void actionPerformed(ActionEvent e) {
        if ("Start" == e.getActionCommand() && pbTask == null) {
            theButton.setText("Stop");
            theButton.setActionCommand("Stop");
            (pbTask = new PBTask()).execute();
        } else if ("Stop" == e.getActionCommand()) {
            theButton.setText("Start");
            theButton.setActionCommand("Start");
            pbTask.cancel(true);
            pbTask = null;
        } else {
            alertMsg("Thread still running.");
        }
        try {
            pbTask.get();
        } catch (InterruptedException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (ExecutionException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        System.out.println("DONE");
    }
    

    This change is just to illustrate the difference. In order to write actual code we need to know more about what you're trying to achieve.


    If my extrasensory skills are ok, then you probably want to flip button back to "Start". In order to do this, you need to override done() method in the Worker:

    private class PBTask extends SwingWorker {
        @Override
        protected Void doInBackground() {
            int prog1 = 0;
            int prog2 = 0;
            Random random = new Random();
    
            while (prog2 < 100) {
                if(prog1 >= 100) {
                    prog1 = 0;
                }
                //Sleep for up to one second.
                try {
                    Thread.sleep(random.nextInt(100));
                } catch (InterruptedException ignore) {}
                //Make random progress.
                prog1 += random.nextInt(10);
                prog2 += random.nextInt(5);
                publish(new UpdatePB(prog1, prog2));
            }
            return null;
        }
    
        @Override
        protected void process(List pairs) {
            UpdatePB pair = pairs.get(pairs.size() - 1);
                pb.setValue(pair.pb1);
                pbF.setValue(pair.pb2);
        }
    
        @Override
        protected void done() {
            super.done();
            theButton.setText("Start");
            theButton.setActionCommand("Start");
        }
    }
    

提交回复
热议问题