• Post Reply Bookmark Topic Watch Topic
  • New Topic

Can't stop a thread..

 
Nicole Gruber
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I have this assignment where I am supposed to create a panel with four buttons. If you click one of them, music should begin and loop continuosly, if you click it once more, the music should stop playing. Unfortunatley, I can't manage to do that (stop the music from playing)..
I have two classes: One is called PlayClip(extends Thread) and the other one SimpleClipPlayer(extends JFrame implements ActionListener).
The first class contains also the method run() :

In the second class are the four buttons and also the method ActionPerformed():


I can't use the stop() method because it's deprecated and interrupt() doesn't seem to work. I also tried to use a flag and I've put the method clip.loop(Clip.LOOP_CONTINUOUSLY); in a while-loop, put that doesn't seem to work either..

Can someone help me?
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It depends entirely on how the clip.loop() method behaves. Can you show us the code, did you write it? If not, where does it come from?

Your first couple strategies are the correct idea:
1) interrupt the Thread. But whatever clip.loop() does would have to respond to interrupts.
2) Use a flag. But you probably want to not use clip.loop(<continuous>) in this case because then you would have no way of checking the flag. Maybe something like:


If clip has a method for looping continuously, does it have a means of stopping the loop?


I also wanted to make some other general comments:

This isn't generally a good idea - or necessary. Instead of comparing this.isRunning == true (which when in habit can lead to the unfortunate mistake of this.isRunning = true) you should get into the habit of using the more concise and less error prone:




This could also be a problem. clip1 is a Thread. If clip1 has already been started, then stopped, then this code gets executed a second time. When that happens the Thread throws an illegal thread state exception because a Thread can only be started once. You would need to dispose of the old thread and run a new one.

It is for reasons like this (amongst other things) that I generally would avoid subclassing Thread. Instead, make your class a Runnable and provide a start() method which generates a new Thread and stop() method which provides a safe method of stoping the task. Something like:



Then call it like:


A little bit of more code may be needed in the start() method to make sure the restarting of a new Thread is safe (and keeps the same clip from playing multiple times), or it might be safe enough to just interrupt the old thread before always starting a new one... whichever seems best to you:
 
Nicole Gruber
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Steve!!! My program works now!
and also thanks for the tips ;) I'm really grateful!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!