0

In my java gui program, I have a button, of which upon clicking, will create a new thread that plays a song in the background. Had I not created a new thread, and just played it in the actionListener, the song would freeze up the rest of the gui to be unusable until it finished.

However, I would like a means for the user to stop the song.. which in code I would take to mean 'terminate the thread'. I've tried some online answers like the deprecated .stop() .interrupt() methods, but they don't seem to work. I think this has something to do with the fact that the thread that plays the song is in Thread.sleep() for the duration of the song.. any way to terminate a sleeping thread? -Thanks

Woodrow
  • 136
  • 2
  • 10
  • 1
    There are a bunch of things you can make use of in the `java.util.concurrent` that might make things easier than dealing directly with a thread lifecycles. Will be hard to know without seeing any code. – mkobit Dec 20 '14 at 04:57
  • 1
    here's an example of interrupting a sleeping thread http://stackoverflow.com/a/5915306/217324 interruption is designed to allow breaking out of sleep or wait states. if IO is involved that may complicate things, but sleep should not be an issue. i suspect you're looking at the wrong problem. – Nathan Hughes Dec 20 '14 at 04:57
  • 1
    OTOH, if the thread has run, and the "music" is now in a buffer held by the OS, then you're going to have a tougher time stopping that. Try to play the sound a little at a time so you can just not feed the audio any more bytes. – markspace Dec 20 '14 at 05:11
  • Thanks for all the advice (everybody who has posted) -- when I get back to my coding computer tomorrow, I'll try out the suggestions. – Woodrow Dec 20 '14 at 05:29

4 Answers4

1

Instead of sleeping, you could have your thread wait for an object to be notified with a timeout. You would then notify the object if you want the thread to wake up or interrupt it.

Kevin Kaske
  • 1,139
  • 12
  • 17
  • Thanks for the suggestion.. the way I've learned to multithread is like this: Thread t1 = new Thread(new MusicPlay()); t1.start(); but then, how would I interact with the MusicPlay thread? Like to call a method of the MusicPlay class from the original thread that would flip a flag variable within the thread playing the song from the original thread? – Woodrow Dec 20 '14 at 04:17
1

You can use interrupt method. It will give interrupt signal to sleep method. you will catch InterruptedException and it will return from method.

Siva Kumar
  • 1,983
  • 3
  • 14
  • 26
  • This is the preferred technique, as it works for both sleep() and while doing most IO. – user949300 Dec 20 '14 at 05:32
  • @user949300 I will work for both sleep and IO. Incase of any method processing just like while(true), then you have a variable isInterrupted by default its false, once you interrupt set into true. if its true break the loop. – Siva Kumar Dec 20 '14 at 09:23
1

You have to use the wait() and notify() function.

wait() tells the calling thread to give up the monitor and go to sleep until some other thread enters the same monitor and calls notify( ).

notify() wakes up the first thread that called wait() on the same object.

So you can keep another button like 'resume button' which triggers the event to notify().

YakuZa
  • 516
  • 7
  • 12
0

Make Use of join method in Main method of your java program. join method waits for a thread to die. Ex.

try  
    {  
        // waiting for thread t1 & t2 to complete its execution  
        thread.join();
        thread2.join();  
        
    }  
    catch (InterruptedException ie)  
    {  
        ie.printStackTrace();  
        
    }  
    System.out.println("T1 "+thread.getState());
    System.out.println("T2 "+thread2.getState());

op: T1 Terminated T2 Terminated

Or visit this code https://onlinegdb.com/Cr1HMdpzcg

for more Multithreading concepts visit https://www.javatpoint.com/multithreading-in-java