Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Stop Server Thread

the following code is server code in my app:

private int serverPort;

private Thread serverThread = null;

public void networkListen(int port){

    serverPort = port;

    if (serverThread == null){
        Runnable serverRunnable = new ServerRunnable();
        serverThread = new Thread(serverRunnable);
        serverThread.start();
    } else {

    }
}

public class ServerRunnable implements Runnable {

    public void run(){

        try {

            //networkConnected = false;
            //netMessage = "Listening for Connection";
            //networkMessage = new NetworkMessage(networkConnected, netMessage);
            //setChanged();
            //notifyObservers(networkMessage);

            ServerSocket serverSocket = new ServerSocket(serverPort, backlog);

            commSocket = serverSocket.accept();

            serverSocket.close();
            serverSocket = null;

            //networkConnected = true;
            //netMessage = "Connected: " + commSocket.getInetAddress().getHostAddress() + ":" +
                    //commSocket.getPort();
            //networkMessage = new NetworkMessage(networkConnected, netMessage);
            //setChanged();
            //notifyObservers(networkMessage);

        } catch (IOException e){

            //networkConnected = false;
            //netMessage = "ServerRunnable Network Unavailable";
            //System.out.println(e.getMessage());
            //networkMessage = new NetworkMessage(networkConnected, netMessage);
            //setChanged();
            //notifyObservers(networkMessage);
        }
    }
}

The code sort of works i.e. if im attempting a straight connection both ends communicate and update.

The issue is while im listening for a connection if i want to quit listening then the server thread continues running and causes problems.

i know i should not use .stop() on a thread so i was wondering what the solution would look like with this in mind?

EDIT: commented out unneeded code.

like image 660
iTEgg Avatar asked May 10 '10 17:05

iTEgg


People also ask

How do I stop a server thread?

Close the server socket from an external thread. As per the documentation on Serversocket. close() the blocking accept will throw a SocketException and you can shutdown your thread.

How do you stop a thread in Java?

Whenever we want to stop a thread from running state by calling stop() method of Thread class in Java. This method stops the execution of a running thread and removes it from the waiting threads pool and garbage collected. A thread will also move to the dead state automatically when it reaches the end of its method.

How do you stop a runnable thread in Java?

But if we want to stop a thread from running or runnable state, we will need to calling stop() method of Thread class. The stop() method is generally used when we desire premature death of a thread. Since the stop() method is static in nature, therefore, it can be called by using Thread class name.


2 Answers

Close the server socket from an external thread. As per the documentation on Serversocket.close() the blocking accept will throw a SocketException and you can shutdown your thread.

like image 94
RedPandaCurios Avatar answered Oct 09 '22 20:10

RedPandaCurios


After initializing your ServerSocket, use setSoTimeout. Put the accept in a loop, catching the timeouts. Break from the loop and return from run based on whether you want to continue or not.

like image 28
Jonathon Faust Avatar answered Oct 09 '22 18:10

Jonathon Faust