I am trying to implement a music player.
I wrote a class which extends from Thread and overwrote its Start()-Method to play a random song.
Playing a song works, but I want to send that thread to the background, which doesn’t work:
File file = new File("song.mp3");
PlayEngine plengine = new PlayEngine(); //This class extends from Thread
plengine.Play(file); //This just sets the file to play in a variable
plengine.Start(); //And this finally plays the file itself
System.out.println("Next task:"); // I don't get to this point. Only when the song has finished.
As you can see in the code above, I’d like to go to the printed line right after launching the thread.
It is not recommended to extend
Thread– Have yourPlayEngineimplementRunnableinstead, and override therunmethod:Then start the tread with:
and
Next taskshould print immediately. In your example, you seem to be calling the long running methodplayin the main thread, which explains why it does not return immediately.