Results 1 to 3 of 3
Thread: Stopping threads
- 12-30-2010, 03:56 PM #1
Member
- Join Date
- Sep 2008
- Posts
- 40
- Rep Power
- 0
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.
Thanks
- 12-31-2010, 02:52 AM #2
maybe in thread B, run() method
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.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()) { threadA.interrupt(); } } }
- 01-06-2011, 03:38 PM #3
Senior Member
- Join Date
- Nov 2008
- Posts
- 286
- Rep Power
- 5
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.Neil Coffey
Javamex - Java tutorials and performance info
Similar Threads
-
Stopping a thread
By Arne in forum Threads and SynchronizationReplies: 9Last Post: 10-21-2010, 10:26 AM -
Stopping a thread
By userj2ee in forum New To JavaReplies: 3Last Post: 08-13-2010, 07:57 PM -
Stopping a .swf with java
By ercarls in forum New To JavaReplies: 2Last Post: 04-14-2010, 06:33 PM -
stopping second time round
By silverspoon34 in forum New To JavaReplies: 0Last Post: 11-20-2009, 02:04 PM -
stopping thread...using flags
By rstepler in forum New To JavaReplies: 1Last Post: 07-31-2008, 09:36 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks