This week's book giveaway is in the Jython/Python forum.
We're giving away four copies of Murach's Python Programming and have Michael Urban and Joel Murach on-line!
See this thread for details.
Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Trying to understand threads...  RSS feed

 
Chris Stewart
Ranch Hand
Posts: 184
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I'm writing a little Applet/Swing app. The main purpose is a game. I'd also like to be playing audio in the background, random clips actually. I was thinking that in my Applet (which starts the app), I would create two Threads and start each. One for the Layout class (has the GUI, events, and all) and one for a Sound class that periodically plays sound clips. Does this sound correct? Should I make the sound thread a daemon thread or just make its permission lower than the Layout thread? Here are some of the classes I've done... (note: I haven't made the sound class yet, does anyone want to offer pointers here?)
Startup.java

Layout.java (not all of it)
 
Chris Stewart
Ranch Hand
Posts: 184
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
?
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think your idea of having a background thread for the audio is probably a good idea. The play() method is already asynchronous. The method spawns a thread and returns immediately, and the sound gets played in a background thread. If you want to have your own background thread that manages the playing of sounds, maybe changing them, etc, having a separate thread is probably a good solution.
But I don't understand what you are trying to do with your layout thread. It doesn't seem to be doing anything that requires an independent thread.
[ June 28, 2003: Message edited by: Rob Ross ]
 
Chris Stewart
Ranch Hand
Posts: 184
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, cool! Should I write a class that is just for the sounds? I'm guessing my run method would pick a random sound from an array or something and play it, then sleep for a few mins. Does that sound about right? Should I make this a daemon thread?
Now that I think about it, I don't know why I was going to give the layout a thread. It can just run like normal.
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Chris Stewart:
Ok, cool! Should I write a class that is just for the sounds? I'm guessing my run method would pick a random sound from an array or something and play it, then sleep for a few mins. Does that sound about right?

That sounds like a good plan to me.

Originally posted by Chris Stewart:
Should I make this a daemon thread?
Now that I think about it, I don't know why I was going to give the layout a thread. It can just run like normal.

I assume you know that all daemon threads end when the main thread ends. The only potential issue I see is that different browsers might interact with your applet in a slightly different manner. If the browser calls stop() when you go to a different page, but only calls destroy() when you quit the browser, then your sound thread could keep playing when you went to a different page. A better way to do this is to have your stop() method change some global stop-thread-flag to true, and in the loop of your sound thread, you check for this and exit when it's true, so your thread dies. This is more reliable than just setting a daemon thread accross multiple browsers. Then when the user comes back to your applet page, they can just click the start button again to start the sound thread, OR you could start it from your applet's start() method.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!