Kevin Moore
Kevin Moore

Reputation: 197

Send data to a specific thread

I want to be able to create multiple threads and send data to a specific thread based on what the main program receives.

Basically I am sending a packet to a receiving program which will contain a number. This number is used to determine which thread it wants to communicate with. How can I send that packet to a thread with that same number?

Example: threads 1,2,3,4 and 5 exist. My main program receives a packet with the number 3. I want to send that packet to thread 3.

How can I achieve this?

Upvotes: 6

Views: 2669

Answers (1)

ziesemer
ziesemer

Reputation: 28687

Create a queue for each thread, and have each thread listen to that queue. Your main thread can then put data (or a "message") on each queue.

You'll just want to make sure that the queue structure you choose is safe for concurrent access (thread-safe). Something like a LinkedBlockingQueue should do nicely.

Upvotes: 6

Related Questions