Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Communicating between two threads

Tags:

I have a thread, A which has a list. List holds some objects. Now I want to design a mechanisim by which I can send some message to thread A.

Thread A runs in a loop (it does not wait or sleep). Some other thread, B, sends some message to thread A and thread A empties all its queues.

How can I send messages between threads?

class A extends Thread {     List<Object> objs = something; //Init it     void run() {         while(true) {             //Body which works on objects.             //After receiving an external message, "A" should perform some action, for example, empty objects.         }     } } 

EDIT: Can I do it like this?

class A extends Thread {     List<Object> objs = something; //Init it     Boolean flag = false;      public void setFlag(boolean value) {         synchronized(flag) {             this.flag = value;         }     }      public void getFlag() {         synchronized(flag) {             return this.flag;         }     }      void run() {         while(true) {             //Body which works on objects.             //After receiving an external message, A should perform some action, for example, empty objects.             if (getFlag == true)                 //Empty list         }     } } 
like image 311
akshay Avatar asked Aug 02 '11 17:08

akshay


People also ask

Is communication possible between two threads of two different processes?

Threads use the memory of the process they belong to. Inter-process communication is slow as processes have different memory addresses. Inter-thread communication can be faster than inter-process communication because threads of the same process share memory with the process they belong to.

Which methods used for inter-thread communication?

InterThread Communication is the process in which two threads communicate with each other by using wait (), notify (), and notifyAll () methods. The Thread which is required updation has to call the wait() method on the required object then immediately the Thread will be entered into a waiting state.

How do you pass data between two threads in python?

You can protect data variables shared between threads using a threading. Lock mutex lock, and you can share data between threads explicitly using queue. Queue.


2 Answers

You could have a BlockingQueue of message objects. Other threads would place messages onto the queue. As part of the while(true) loop, thread A would poll the queue and process any messages that have arrived.

In code:

class A extends Thread{  List<Object>  objs = something ;//init it  BlockingQueue<Message> queue = new LinkedBlockingQueue<Message>();  void run(){      while(true){        Message msg;        while ((msg = queue.poll()) != null) {          // process msg        }        // do other stuff      }    } } 

Other threads can now call queue.put() to send messages to thread A.

like image 70
NPE Avatar answered Feb 07 '23 05:02

NPE


In a simplistic case you can add some instance variable to thread A class and have thread B set its value to indicate that thread A must clear its' queues. In more advanced case you can use some queue of messages that both threads A and B can access. B would place a message there and A would read and act on it.

In all cases, access to variable or queue must be properly guarded for multiple threads access.

like image 40
Alex Gitelman Avatar answered Feb 07 '23 05:02

Alex Gitelman