wood burning stoves*
The moose likes Threads and Synchronization and the fly likes Multi-Thread Help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Multi-Thread Help" Watch "Multi-Thread Help" New topic
Author

Multi-Thread Help

Qunfeng Wang
Ranch Hand

Joined: Jan 28, 2005
Posts: 433
Question:
When an alert raises, my client starts a new thread and palys a .wav file within it. Then if the user wants to stop the music playing, how can I know which thread to stop and how to do it?

Thanks!


To be or not to be. It's a question.
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
How are you starting it? You'll have to keep a reference to the Thread or Runnable and implement a way of stopping it. Those are potentially two separate issues. I'd start with figuring out how you can keep track of the Thread or Runnable, then worry about how you can get it to stop playing.
Qunfeng Wang
Ranch Hand

Joined: Jan 28, 2005
Posts: 433
Yeah, the key is how to keep track of the Thread. It's easy to stop it when I've already know Which thread it is.

So what your suggestions on this problem?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18765
    
  40

As Ken said, you need to have a reference to it... Basically, when your client creates the thread, it will need to store it somewhere, that can be accessed by another thread, when you need to stop it. Maybe in some static variable somewhere.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
Originally posted by Loius Wan:
Yeah, the key is how to keep track of the Thread. It's easy to stop it when I've already know Which thread it is.

So what your suggestions on this problem?


I think you'd have to elaborate on your design and how you're starting this to answer that. It could be stored in static or instance variable, maybe put in a map, it's hard to say with so little info.
Qunfeng Wang
Ranch Hand

Joined: Jan 28, 2005
Posts: 433
Thank you for your reply.
I'm developing Fault Management in NMS(Network Management System).The original design is When an alert araises, my client will catch it and play a .wav file in a thread. The thread will exit when the .wave file arrives its' end. This means I need not bother to stop by myself.Now the requiements change. Whan an alert arrives, I have to loop the .wav file until the user conform or clear the alert. In this case, let me say, the sound thread, has to run until some thread infor it to stop. This means I have to stop the thread by my own.

I'v read some artices on this subject, with your suggestions I have an idea like this:


Is that right?
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
First, and this is important, you should not be using stop(). It is deprecated and there is an explanation as to why. Put your code in a loop and have it based on a condition you can change. For example:



Note that it won't stop immediately, just the next time it goes through the loop and checks the condition. Furthermore, it'd probably be possible for it to observe running as true even after shutdown() had been called. If it's critical that something shutdown immediately this should be properly synchronized, but it should give you an idea of what I mean.

That's one way you might do it. Here's a thought though. If you're able to keep a reference to "threadName" in order to retrieve it from the HashTable then why not just keep a reference to the Thread instead? The only reason I would use something like a HashTable is if you can't hold on to a reference to it but you are able, in some way, to know what the appropriate key is.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
How are you playing the wav files, anyway? Seems to me that whatever API you're using for this ought to have a way of stopping play without you having to do thread management. E.g. a javax.sound.sampled.Clip has a start() and stop() method - but these have nothing to do with the methods of the same name in the Thread class. Well, there may be other threads involved behind the scenes, but you probably don't need to deal with them. I think. So - what API are you using to play the clip in the first place?


"I'm not back." - Bill Harding, Twister
Qunfeng Wang
Ranch Hand

Joined: Jan 28, 2005
Posts: 433
I use java.awt.AudioClip
Yeah, it has stop() method. If I can keep track of the AudioClip, then I can stop it by its stop() method. I have to put it into a hashtable with an unique key. When come to stop time, I get it out and stop it with stop() method.

As you said, it manages thread issues by its own. So I need not borhter to do thread issues. Let me try it now! Thank you all.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Multi-Thread Help