user573382
user573382

Reputation: 343

Simple way to wait for a method to finish before starting another one

I'm not familiar at all with Java threading :(. I have this class which, when called, constructs a new window (draw() method). The drawGUI() calls a processing method at the end (compare() method).

basically the structure is

public static void draw() {
    // draws stuff


    compare();
}

The problem is that the window drawn by drawGUI() has some major visual artifacts till the processing (compare() ) is over.

What is the simplest way I can implement to launch compare() after draw() has finished executing? Thank you

Upvotes: 4

Views: 15908

Answers (2)

Ovi Tisler
Ovi Tisler

Reputation: 6473

The simplest way is to just put your draw() code inside an asyncExec() inside your thread at the end

new Thread(new Runnable() {
public void run() {

    //do long running blocking bg stuff here
    Display.getDefault().asyncExec(new Runnable() {
        public void run() {
            draw();
        }   
    }
}).start();

Upvotes: 3

Vala
Vala

Reputation: 5674

Assuming that the reason you're getting the artefacts is that draw() hasn't had a chance to return, you can use a Thread.

final T parent = this;
new Thread(new Runnable() {
    public void run() {
        parent.compare();
    }
}).start();

(Where T is the type of the class that has your compare method).

Upvotes: 1

Related Questions