Results 1 to 5 of 5
  1. #1
    phil128 is offline Member
    Join Date
    Sep 2008
    Rep Power

    Default Stopping threads

    Hello. I need help on stopping threads, but my problem is explaned below:

    Thread A access the sound card to play a wav file (given the file name).
    Thread B starts Thread A (I still need my GUI to respond to input)

    So what I'm trying to do is when I click on my button it spawns Thread B, which Spawns Thread A. Now when I click the button again I want the thread to stop, i.e if I decided that I want to stop playing the WAV file half way through I can by stopping Thread B.

    I cannot figure out HOW I will go about this. I'm trying to do it, but I'm getting no where.


  2. #2
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Rep Power


    maybe in thread B, run() method

    Java Code:
      // assume you have a threadA reference inside thread b
      Thread threadA; 
     public void run() {
       try {
         // the stuff thread B does normally
       } catch (InterruptedException ex) {
          // we got interrupted
       finally {
        // try to stop  thread A when we are interrupted, or if this thread naturally exits.
         if (threadA != null && threadA.isAlive()) {
    where to invoke this, the thing that started threadB could do a threadB.interrupt() to have it stop, which should now attempt to stop threadA as well.

  3. #3
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Rep Power

    Default You need to stop threads "cleanly"

    In general, you need to stop threads "cleanly" not by actually stopping the thread, but by signalling to it that it needs to stop what it's doing. For an overview, you may like to look at my own article on stopping a thread in Java-- however, the bottom line is that you need to arrange for the thread's run() method to exit.

    Now, in your case, you're presumably using some kind of library to play the WAV file. So you need to look at what facilities that library has. If you're able to give more information about what code/library you're using to play the WAV file, then we may be able to be of more specific help.

  4. #4
    Ajk_P is offline Member
    Join Date
    Feb 2014
    Rep Power

    Default Re: Stopping threads

    You might want to use locks wait and notify. (i.e. when you click notify the correct thread, and wait the correct thread
    Synchronization in Java

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Northern Virginia, United States
    Rep Power

    Default Re: Stopping threads

    You're about 3 years too late!

    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Stopping a thread
    By Arne in forum Threads and Synchronization
    Replies: 9
    Last Post: 10-21-2010, 10:26 AM
  2. Stopping a thread
    By userj2ee in forum New To Java
    Replies: 3
    Last Post: 08-13-2010, 07:57 PM
  3. Stopping a .swf with java
    By ercarls in forum New To Java
    Replies: 2
    Last Post: 04-14-2010, 06:33 PM
  4. stopping second time round
    By silverspoon34 in forum New To Java
    Replies: 0
    Last Post: 11-20-2009, 03:04 PM
  5. stopping thread...using flags
    By rstepler in forum New To Java
    Replies: 1
    Last Post: 07-31-2008, 09:36 PM

Posting Permissions

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