Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to start a background thread that doesn't block the main thread in Java?

I have the following Java code:

public static void main(String[] args)
{
    new Thread(new MyRunnable()).run();
    showGUI();
}

My problem is that starting MyRunnable blocks the main thread, causing showGUI to not be called until it finishes running. What I'd like the program to do is spawn MyRunnable and allow it to run independently in the background, enabling the main thread to forget about it and go ahead and do what it wants (like call showGUI).

like image 453
XåpplI'-I0llwlg'I - Avatar asked Nov 27 '22 17:11

XåpplI'-I0llwlg'I -


1 Answers

run executes on the main thread. start will create a new thread execution and execute it's run method on that thread.

like image 81
John Vint Avatar answered Dec 04 '22 01:12

John Vint