Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 774
Kevin Moore Avatar asked Nov 29 '11 02:11

Kevin Moore


1 Answers

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.

like image 119
ziesemer Avatar answered Oct 23 '22 04:10

ziesemer