0

I have a method that can take time to complete as it uses the internet to check something and depending on the internet speeds can take a while so I have placed it into its own thread but I'm wanting to know how I can notice when this thread is complete as I then need to update the screen from the Main thread.

New thread code looks like this:
Thread newThread = new Thread(){
             @Override
                public void run() {

//My thread code
};
    newThread.start();

This is what the thread code looks like, not going to copy and paste all the code it uses as it would be pointless but I'm wanting something like when

newThread.iscomplete {
//More code
}

Something along those lines

Thanks

Touchstone
  • 5,575
  • 7
  • 41
  • 48
user3519364
  • 79
  • 1
  • 6
  • 3
    Why not simply update the screen from the spawned thread, when it has the result? Or pass a callback object to the spawned thread that will be called by it when it ends? – JB Nizet May 29 '14 at 15:43
  • If you want to do something at the end of a thread, you can add it to the code it runs at the end. – Peter Lawrey May 29 '14 at 15:46

1 Answers1

0

Maybe you can show a message at the end of the Thread. Which would be helpful to let the user know that the process has finished.

Showing Message

System.out.print("Thread has finished!");

This would be at the last line, so it would only execute if all the above codes have been executed and the code has reached the last line for execution.

Thread newThread = new Thread() {
    @Override
    public void run() {
        //My Thread code
        System.out.print("Done!");
    }
};

This will print Done! in the Console (I assume application is being run using a Console). Denoting that the code has been executed.

using a variable

boolean done = false;
Thread newThread = new Thread() {
    @Override
    public void run() {
        //My Thread code
        done = true;
    }
};

Using EventListeners

Or else you can use Event Listener to detect the Thread completion. The above method was kind of simple.

http://docs.oracle.com/javase/tutorial/uiswing/events/

Braden Steffaniak
  • 2,053
  • 3
  • 25
  • 37
Afzaal Ahmad Zeeshan
  • 15,669
  • 12
  • 55
  • 103