Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can 2 threads communicate each other?

Thread A is summing up data passed from 10 clients.

while(true){
    Socket clientfd= server.accept ();
    BufferedReader message = new BufferedReader(new InputStreamReader (clientfd.getInputStream() ) );
    String val = message.readLine();
    this.sum_data+=(message.readLine();
    message.close ();
    clientfd.close ();
    this.left--;
    if(this.left==0){
        System.out.println(this.sum_data);
        break;
    }
}

Thread B is constantly communicating with clients whether they are alive or not (heartbeating technique).

The thing is that clients sometimes can fail, and in that case, thread which is summing up data should just print out the all possible results from alive clients. Otherwise, it will never printout the result.

So, if heartbeat thread notices one client is not responding, is there a way for it to tell the other thread (or change other thread's class variable this.left)?

like image 287
user482594 Avatar asked Jul 31 '11 01:07

user482594


People also ask

Can we communicate between threads?

The easiest way for multiple threads to communicate is through memory. If two threads can access the same memory location, the cost of that access is little more than the memory latency of the system.

How do two threads communicate with each other in C?

producer and consumer threads should capture each other's tid. producer on producing can send: pthread_kill(consumerID, SIGUSR1); consumer is setup with the signal handler for SIGUSR1, and can retrieve the produced result from the common std::queue saved by pthread_setspecific().

How do threads communicate in OS?

Threads are not independent of one another like processes are, and as a result threads share with other threads their code section, data section, and OS resources (like open files and signals). But, like process, a thread has its own program counter (PC), register set, and stack space.

How do threads share data with one another?

All static and controlled data is shared between threads. All other data can also be shared through arguments/parameters and through based references, as long as the data is allocated and is not freed until all of the threads have finished using the data.


1 Answers

Basically, there are two general approaches to thread communication:

  1. Shared memory
  2. Event/queue based

In the shared memory approach, you might create a a synchronized list or a synchronized map that both threads may read from and write to. Typically there is some overhead to making sure reads and writes occur without conflicts, you don't want to have an object you're reading deleted while you're reading it, for instance. Java provides collections which are well behaved, like Collections.synchronizedMap and Collections.synchronizedList.

In event, or queue based, thread communication, threads have incoming queues and write to other thread's incoming queues. In this scenario, you might have the heartbeat thread load up a queue with clients to read from, and have the other thread poll/take from this queue and do its processing. The heartbeat thread could continually add the clients that are alive to this queue so that the processing thread "knows" to continue processing them.

like image 89
Mark Elliot Avatar answered Oct 17 '22 04:10

Mark Elliot