I have a main thread, and inside this thread I start a new thread. (child thread). This child stream opens the server socket and starts listening on the connection. I want this thread to stop executing and close everything that it initialized (for example, Socket) when the main thread receives the message from the outside (where it receives the message from is not a problem). How should I stop the thread and close all the connections that I want.
Should I use a shared variable? so when the main thread receives the message, it must change it, and the child thread must constantly check for changes in this shared variable?
How do I implement it? Some useful links might help or sample code?
What I tried looks like this: in the main thread, I 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 code above does not work at all. How can I do it?
neerajDorle
source share