Link to home
Start Free TrialLog in
Avatar of Reggie_BE
Reggie_BE

asked on

Stop, and restart Threads

I've made a thread to run my server. It looks like this:

serverThread = new Thread(new Runnable() {
                public void run() {
                    server = new RiotServer();
                    server.setStatusWindow(txtStatus);
                    server.setConvoWindow(txtConvo);
                    server.startServer();
                }          
});


private void tglConnectActionPerformed(java.awt.event.ActionEvent evt) {                                          
        if (tglConnect.isSelected()) {
            tglConnect.setLabel("Disconnect");
            serverThread.start();    
        } else {
            tglConnect.setLabel("Connect");
            serverThread.stop();
            server.stopServer();
        }
}

Now I have a button to connect and disconnect. When I press connect I call serverThread.start();
When I press disconnect I tried serverThread.stop(); Without success, 'cause when I run serverThread.start() again, he gives me an IllegalThreadStateException.


I've googled it up, but I still didn't found any solution .. is there any way to completely stop the Thread ?
Avatar of Mick Barry
Mick Barry
Flag of Australia image

You cannot restart a stopped thread, you need to recreate a new thread.
stop() is also deprectaed and its use should be avoided.
Avatar of Reggie_BE
Reggie_BE

ASKER

Maybe better if I work with ExecutorService and Threads ?
If you just want pause the thread then use wait() and notify()
Kay, I tried it another way .. but erm, now I have another problem.
I have this methods setStatusWindow(JTextPane window), printStatusMessage(String message). In the constructor of the gui I run setStatusWindow .. and then printStatusMessage ... and I get a NullPointerException... is it because of the executorService ?

Here's the code:

RiotServer.class

public class RiotServer extends Thread {
   
    private static ServerSocket sock;
    private static final int PORT = 1800;
    private ExecutorService pool = Executors.newCachedThreadPool();
    private Vector<RiotClientHandler> clientHandlers = new Vector<RiotClientHandler>();
   
    private JTextPane statusWindow;
    private JTextPane convoWindow;
               
    /** Creates a new instance of Riotserver */
   
   
    public RiotServer() {
    }
   
    public void run() {
        startServer();
    }
   
    private void startServer() {
       
      printStatusMessage("Starting server ...");
        try {
            sock = new ServerSocket(PORT);
            printStatusMessage("SUCCESS ! Socket is open (" + PORT + ")");
            do {
                acceptConnections();
            } while (true);
        } catch (IOException e) {
            printStatusMessage("ERROR ! Socket is already open or inaccessible (" + PORT + ")");
        }
    }
   
    private void stopServer() {
       
        printStatusMessage("Terminating server ...");
       
        try {
            sock.close();
            sock = null;
            pool = null;
            clientHandlers.clear();            
        } catch (IOException e) {
           
        }
    }
   
    public void setStatusWindow(JTextPane statusWindow) {
        this.statusWindow = statusWindow;
    }
   
    public void setConvoWindow(JTextPane convoWindow) {
        this.convoWindow = convoWindow;
    }

    private void acceptConnections() {
        Socket link = null;
        try {
            Socket client = sock.accept();
            printStatusMessage("*** [JOIN] "+client.getInetAddress());
            RiotClientHandler handler = new RiotClientHandler(client,this);
          clientHandlers.add(handler);
            pool.execute(handler);
        } catch(IOException ioe) {
            printStatusMessage(ioe.getMessage());
      }
    }

    public void printStatusMessage(String message) {
        // ERROR NullPointerException ...
        this.statusWindow.setText(statusWindow.getText()+message+"\n");
    }
   
    public void printConvoMessage(String message) {
        this.convoWindow.setText(convoWindow.getText()+message+"\n");
    }
   
    public void echoToClients(String s){
      for (RiotClientHandler clh : clientHandlers) {
            clh.echoToClient(s);
      }
    }
}


In the GUI I have this:

   public RiotServerGUI() {
       
        pool = Executors.newCachedThreadPool();
       
        guiThread = new Thread(new Runnable() {
            public void run() {
                initComponents();
            }
        });
       
        guiThread.start();
       
        server = new RiotServer();
        server.setStatusWindow(txtStatus);
        server.setConvoWindow(txtConvo);
       
       
    }
Thats because you call them beofre statusWindow has been set.
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Then the window just stays empty .. I call the printMessage for the first time when I press a "Connect" button.
if it doesn't know about the window then it cannot display to it :)

Perhaps pass the window in the ctor.
Found it. I'm using guiThread.start() ... replaced it with run().
Thanks for your time objects !
> I'm using guiThread.start() ... replaced it with run().

That doesn't start a new thread, it calls the run() method of your thread instance (on the same thread)