Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Communication between parent and child thread in Java

I have got a main thread and within that thread I start a new thread. (the child thread). That child thread opens a server socket and starts listening for a connection. I want that thread to stop its execution and close whatever it has initialized (like the Socket) when the main thread gets a message from outside (from where it gets the the message is not the concern). How should I stop the thread and close all the connections is what I want.

Should I use a shared variable? so that when the main thread receives the message it should modify it and the child thread should continually check for the changes in that shared variable?

How should I implement it? Some useful links may help or a sample code ?

What I have tried is as follows: in the main thread I have declared a variable

 flag=0;

when the main thread receives the message, it sets

flag = 1 ;

and the thread listens for the change as follows:

  void ()run{

       while(true){

            if(flag==1){
                   break;
              }

       sock1 = Ssocket.accept(); 
  }

But the above code is not at all working. How should I do it?

like image 662
neerajDorle Avatar asked May 06 '13 11:05

neerajDorle


People also ask

How do Java threads communicate with each other?

All the threads in the same program share the same memory space. If an object is accessible to various threads then these threads share access to that object's data member and thus communicate each other. The second way for threads to communicate is by using thread control methods.

How do you communicate between two threads?

Cooperation (Inter-thread communication) is a mechanism in which a thread is paused running in its critical section and another thread is allowed to enter (or lock) in the same critical section to be executed.It is implemented by following methods of Object class: wait() notify() notifyAll()

Can parent thread create child threads?

Thread CreationThe creating thread is the parent thread, and the created thread is a child thread. Note that any thread, including the main program which is run as a thread when it starts, can create child threads at any time.

Do threads have parent/child relationship?

Practically yes. Pretty much every pthreads implementation out there supports it. Provided the "parent" thread doesn't complete before the newly created thread which might still be using the stack address(es) from the parent, this is fine.


2 Answers

The proper way to interrupt a thread is via the interruption mechanism. In your main thread, when you want to stop the child thread, you call:

childTread.interrupt();

and in the child thread, you do something like:

public void run() {
    try {
        while (!Thread.currentThread.isInterrupted) {
            sock1 = Ssocket.accept();
            //rest of the code here
        }
    } catch (InterruptedException e) {
        Thread.currentThread.interrupt(); //good practice
    }
    //cleanup code here: close sockets etc.
}

Note that Ssocket.accept isn't interruptible, so if you want to stop it from waiting, you will have to close it from outside, to force it to throw an IOException.

like image 131
assylias Avatar answered Oct 11 '22 08:10

assylias


Child thread

You should make a new function here, f.e:

public void setFlag(int i)
  {
     flag = i;
  }

Parent Thread

Whenever you want to kill/stop listening/... in the child thread, make a call to:

 childThread.setFlag(1);

If you don't need the child Thread to be anonymous, create a ChildThread class:

 public ChildThread implements Runnable
{
    private int flag = 0;

    public ChildThread()
     {  }

    public void setFlag(int i)
      {
         flag = i;
      }
    public void run()
      { 
       //your code
      }
    ....
}
like image 44
aran Avatar answered Oct 11 '22 08:10

aran