• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Multi-Thread Help

 
Qunfeng Wang
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21490
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Qunfeng Wang
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic