Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to interrupt a thread if it is to open socket?

I have tried to close the current thread that is a part of multi-threading server. The thread is ready to open the socket that may be accessed by clients.

Everything works fine except when the code below is contained in while() loop.

new ServerThread(serversocket.accept(), this.Rstr, bag.numberofDatatoAcquire).start();

Here is the code for the server:

public void run() {

    System.out.println("This has been called ");   

    try{

       System.out.println("This has been tried");    
       serversocket = new ServerSocket(this.iPort);                                 

       Thread thisThread = Thread.currentThread();

       while(!thisThread.isInterrupted()){
           new ServerThread(serversocket.accept(), this.Rstr, bag.numberofDatatoAcquire).start();
           //sending socket accessed, where it will store the data and how much it will collect it.                            
           System.out.println("This has been running");                  
           Thread.sleep(10);               
       }           
    }catch(InterruptedException e){
        //why bother? it is an usual happening...lol
    }catch(IOException ioe)
    {
        System.err.println("Can't open the socket on port");               
    }
    finally{     
       System.out.println("Thread is dead and ready for change");
    }
}

And this is a part of GUI events: this works well without "new ServerThread..." code.

private OverWatch EW = new OverWatch(bag.iPortOrder, bag.SessionAcquisitionSavingDirectory);   

....

private void OverWatcherControl(boolean checker)
{
    if(checker)
        EW.start();  
    else
        EW.interrupt();

}

Since the variable bag.numberofDataToAcquire (public integer type) is supposed to be changed whenever user wants, I think I have to stop this thread and change the variable then run this thread again. Am I wrong? Or how can I interrupt this thread?

Thanks,

like image 941
user1098761 Avatar asked Mar 13 '12 19:03

user1098761


People also ask

How a thread can be interrupted?

A thread can send an interrupt by invoking interrupt on the Thread object for the thread to be interrupted. This means interruption of a thread is caused by any other thread calling the interrupt() method. void interrupt() - Interrupts the thread.

Does interrupt terminate a thread?

interrupt() does not interrupt the thread, it continues to run.

What happens when a thread is interrupted?

An InterruptedException is thrown when a thread is interrupted while it's waiting, sleeping, or otherwise occupied. In other words, some code has called the interrupt() method on our thread. It's a checked exception, and many blocking operations in Java can throw it.


1 Answers

ServerSocket.accept() is a blocking call that is not responsive to thread interruption. Almost all the java.net blocking calls (connect, read, accept, etc) do not respond to Thread.interrupt(). This behavior is "as designed".

A way to wake up a thread blocked in .accept() or .read() is to close the underlying socket.

Alternatively you could set SO_TIMEOUT (setSoTimeout) on the ServerSocket, which will cause .accept() to wake up periodically by throwing a SocketTimeoutException. You could catch that exception and use it as an opportunity to check the interrupt status on the thread.

The java.nio package (Java 1.4+) provides an alternate sockets API that is more responsive to interruption.

like image 192
Mike Clark Avatar answered Sep 19 '22 03:09

Mike Clark