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

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.

Answers


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.


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");
    }
}

Need Your Help

How to access cordova file:/// image with BitmapFactory

java ios image cordova

I'm using a cordova google maps plugin and developed some sort of image cache for displaying markers.

Multilanguage sites: Left-to-right and right-to-left

javascript arabic multilanguage right-to-left

I see some good suggestions in SO for creating multilanguage websites in JavaScript including this article on JavaScript internationalization. However, I am finding it more difficult to determine ...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.