Either I don't actually understand multithreading, or something weird is happening.
I have the following piece of code:
public void playMusic(){
new Thread(new Runnable(){
public void run(){
tune.play();
}
}).run();
System.out.println("*******");
}
This method plays a piece of music. It starts a new thread and does the music-playing in there, to not pause the execution of the current thread.
As such I would expect System.out.println("*********");
to be executed almost immediately when the method is called, since the lengthy operation tune.play()
is invoked on a different thread.
However in practice *********
is printed to the screen only when the music ends.
How do you explain this? And how can I separate the music-playing from the current thread?
If it makes a difference, the 'current thread' is the Swing EDT.