Results 1 to 4 of 4
Thread: Thread help - how to stop?
- 04-10-2011, 10:50 AM #1
Member
- Join Date
- Mar 2011
- Posts
- 13
- Rep Power
- 0
Thread help - how to stop?
In my application I have an audio clip looping for some background music.
I start the music in a new thread.
For the life of me I cant figure out how to stop the thread/music!
Java Code:Thread music = new Thread(new musicTrack(), "trax"); music.start(); public class musicTrack implements Runnable{ Thread music; String soundclip; Clip clip; public void run(){ try { File soundFile = new File("assets/sounds/music2-1.wav"); AudioInputStream audioIn = AudioSystem.getAudioInputStream(soundFile); clip = AudioSystem.getClip(); clip.open(audioIn); clip.loop(Clip.LOOP_CONTINUOUSLY); } catch (UnsupportedAudioFileException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (LineUnavailableException e) { e.printStackTrace(); } } }
- 04-10-2011, 11:02 AM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Have you tried checking the API and the threading tutorials on oracles site? I believe interrupt will stop a thread, but I have not used threads much and have only read a little bit on them. I'm sure the API and tutorials will have an exact answer.
Last edited by sunde887; 04-10-2011 at 11:05 AM.
- 04-10-2011, 12:45 PM #3
Member
- Join Date
- Mar 2011
- Posts
- 13
- Rep Power
- 0
I have done loads of reading and experimenting.
Often times I need an example to make things clear to me. I am probably just doing something tiny that is wrong.
- 04-10-2011, 01:14 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,412
- Blog Entries
- 7
- Rep Power
- 17
As was stated in the API documentation for the Tread class, stopping it in any way from another Thread is not really safe, i.e. resources may get lost (open files or sockets etc.), so your Thread has to cooperate a bit; it can be asked to stop itself so it can clean up all its resources and die a graceful death. Somewhere in the Runnable part of your Thread you have to accomplish that:
You need to extend the class above and implement the two abstract methods. Make sure that the doSomeChunkOfWork() method doesn't tie up the processor too much or otherwise the stopping of the Thread can be slow.Java Code:public abstract class StoppableRunnable implements Runnable { private boolean stop; public void stop() { stop= true; } public void run() { while (!stop) { doSomeChunkOfWork(); } cleanUp(); } abstract protected void doSomeChunkOfWork(); abstract protected void cleanUp(); }
Alternatively you can interrupt() your Thread but then it has to check its isInterrupted() method and be prepared to catch an Exception when it was blocked for some reason (waiting for something or sleeping etc.)
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
How to stop Thread
By ersachinjain in forum Threads and SynchronizationReplies: 2Last Post: 11-30-2009, 07:11 PM -
Thread won't stop
By bubbless in forum Threads and SynchronizationReplies: 15Last Post: 10-20-2009, 10:58 PM -
Help to stop a thread
By raghu_lzybns in forum New To JavaReplies: 4Last Post: 07-09-2009, 04:39 PM -
[SOLVED] How to stop a thread
By AlejandroPe in forum New To JavaReplies: 5Last Post: 04-29-2009, 03:05 PM -
how to stop a thread
By willemjav in forum Advanced JavaReplies: 19Last Post: 09-10-2008, 07:11 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks