Learning Threads - Running a method only after the other has finished

社会主义新天地 提交于 2019-12-11 17:33:25

问题


In my button execution, I am calling 2 methods.

plotButton.addSelectionListener(new SelectionAdapter() {
    public void widgetSelected(SelectionEvent e) {
        startPrinterListOperation();  
        showAplotPlotterDialog();
    }
});

The startPrinterListOperation(); takes some time to complete its task. But I do not want the method showAplotPlotterDialog(); to run until the first one has completed. So I am trying to use thread management to achieve this. Here is what I have tried.

plotButton.addSelectionListener(new SelectionAdapter() {
    public void widgetSelected(SelectionEvent e) {
        Thread t = new Thread() {
            public void run() {
                startPrinterListOperation();  
            }
        };
        t.start();
        try {
            t.join();
        }
        catch (InterruptedException e1) {
            e1.printStackTrace();
        }

        showAplotPlotterDialog();

    }
});  

But the second method stills starts before the first one has finished.


回答1:


I would not bother with threads, this will just make your program overly complicated.

Can you edit the startPrinterListOperation() method?

I would instead add showAplotPlotterDialog(); to the end of the startPrinter method, and the last last thing the method does.




回答2:


Extending on my comment: Seems like startPrinterListOperation launches an asynchronous operation and finishes instantly, evidented by the join succeeding.

If the launched async op is out of your control, then you might be able to observe it finishing via some callback, polling, etc. Then you may employ something like the following in startPrinterListOperation:

void startPrinterListOperation() {
    final CountDownLatch c1 = new CountDownLatch(1);
    launchTheAsyncOp(new SomeCallback() {
        void x() {
            c1.countDown();
        }
    });
    try {
        c1.await(999, TimeUnit.SECONDS)
    }
    catch (InterruptedException e) {
        throw new MyRuntimeException("user didn't finish the op in 999 seconds, fail");
    }
}



回答3:


Answering your general question in the title, you have a master thread that calls your two methods directly, so that the second method waits for the first method to complete.

I understand that in your specific case, the first method runs for a while, and you would prefer that the user not have to wait.

You should call a generatePrinterList() method in a separate thread while you're constructing the GUI. You do this because your GUI users are very likely to print or plot, and the printer list is not likely to change while the user is using your GUI.

Odds are that the generatePrinterList() thread will finish long before your user has to print or plot. But just to be sure, the thread has to have a way of reporting back that the thread is completed. I use a boolean isCompleted that can be read with a public isCompleted() method.

The isCompleted() method could have a thread sleep loop if you want, so it always returns true. In this case the method doesn't have to return anything.



来源:https://stackoverflow.com/questions/12321978/learning-threads-running-a-method-only-after-the-other-has-finished

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!