• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 222
  • Last Modified:

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 ?
0
Reggie_BE
Asked:
Reggie_BE
  • 6
  • 5
1 Solution
 
objectsCommented:
You cannot restart a stopped thread, you need to recreate a new thread.
stop() is also deprectaed and its use should be avoided.
0
 
Reggie_BEAuthor Commented:
Maybe better if I work with ExecutorService and Threads ?
0
 
objectsCommented:
If you just want pause the thread then use wait() and notify()
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Reggie_BEAuthor Commented:
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);
       
       
    }
0
 
objectsCommented:
Thats because you call them beofre statusWindow has been set.
0
 
objectsCommented:
maybe use:

if (statusWindow!=null) {
  this.statusWindow.setText(statusWindow.getText()+message+"\n");
}
0
 
Reggie_BEAuthor Commented:
Then the window just stays empty .. I call the printMessage for the first time when I press a "Connect" button.
0
 
objectsCommented:
if it doesn't know about the window then it cannot display to it :)

Perhaps pass the window in the ctor.
0
 
Reggie_BEAuthor Commented:
Found it. I'm using guiThread.start() ... replaced it with run().
0
 
Reggie_BEAuthor Commented:
Thanks for your time objects !
0
 
objectsCommented:
> 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)
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now