Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Help in Multi Threading!  RSS feed

 
Sundar Ram
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Iam working on creating an audioplayer applet.


I face some difficulties in multi threading in java. The problem i need to solve is
explained below,

There are three threads in the player.
1) Master Thread
2) Buffer Thread
3) Play Thread

The master thread should control the other two threads. So Master thread should
communicate to the Bufffer thread and wait for a response. As soon as it gets the
response from the Buffer Thread, the master thread should communicate to the Play thread.
After playing the data, the play thread should send back a message to the master thread.
Till this message is recieved the master thread should wait.

So the question i have is "How to communicate between threads? How can we make a thread
wait for other threads response?"

Sample code, Links Or help in any form is highly appreciated.

Thanks in advance

-Sundar
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might look into queues so the buffer thread puts chunks of data on a queue and the player thread pulls them off. If the buffer gets ahead (a good thing) chunks of data will pile up in the queue. If the buffer gets behind (a bad thing) the player will block waiting for a chunk.

Your master could stop the buffer so it doesn't read and buffer any more data, or stop the player to let more chunks buffer up, but it wouldn't be involved in moving data from the buffer to the player.

I made a generic PipeStage interface that connects a graph of threads with queues. I had in mind to build filters, modifiers, splitters, joiners, etc for MIDI messages but the latency is a tad high for real time messages. Queued data ought to be just fine.

That feels pretty big for an interface. Maybe you could pare it down or split it up.

Any of that sound useful?
 
Edward Harned
Ranch Hand
Posts: 291
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to get familiar with how threading works. For waiting/notifying threads you need to read the sun tutorial on threading.

Start here:
http://java.sun.com/javase/sitemap.jsp
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!