Results 1 to 4 of 4
  1. #1
    scheffetz is offline Member
    Join Date
    Mar 2011
    Posts
    13
    Rep Power
    0

    Default 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();
          }
        }
      }

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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.

  3. #3
    scheffetz is offline Member
    Join Date
    Mar 2011
    Posts
    13
    Rep Power
    0

    Default

    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.

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,009
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by scheffetz View Post
    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.
    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:

    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();
    }
    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.

    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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. How to stop Thread
    By ersachinjain in forum Threads and Synchronization
    Replies: 2
    Last Post: 11-30-2009, 07:11 PM
  2. Thread won't stop
    By bubbless in forum Threads and Synchronization
    Replies: 15
    Last Post: 10-20-2009, 10:58 PM
  3. Help to stop a thread
    By raghu_lzybns in forum New To Java
    Replies: 4
    Last Post: 07-09-2009, 04:39 PM
  4. [SOLVED] How to stop a thread
    By AlejandroPe in forum New To Java
    Replies: 5
    Last Post: 04-29-2009, 03:05 PM
  5. how to stop a thread
    By willemjav in forum Advanced Java
    Replies: 19
    Last Post: 09-10-2008, 07:11 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •