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(txt Status);
server.setConvoWindow(txtC onvo);
server.startServer();
}
});
private void tglConnectActionPerformed( java.awt.e vent.Actio nEvent evt) {
if (tglConnect.isSelected()) {
tglConnect.setLabel("Disco nnect");
serverThread.start();
} else {
tglConnect.setLabel("Conne ct");
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 IllegalThreadStateExceptio n.
I've googled it up, but I still didn't found any solution .. is there any way to completely stop the Thread ?
serverThread = new Thread(new Runnable() {
public void run() {
server = new RiotServer();
server.setStatusWindow(txt
server.setConvoWindow(txtC
server.startServer();
}
});
private void tglConnectActionPerformed(
if (tglConnect.isSelected()) {
tglConnect.setLabel("Disco
serverThread.start();
} else {
tglConnect.setLabel("Conne
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 IllegalThreadStateExceptio
I've googled it up, but I still didn't found any solution .. is there any way to completely stop the Thread ?
ASKER
Maybe better if I work with ExecutorService and Threads ?
If you just want pause the thread then use wait() and notify()
ASKER
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.newCachedThreadP ool();
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("Starti ng server ...");
try {
sock = new ServerSocket(PORT);
printStatusMessage("SUCCES S ! Socket is open (" + PORT + ")");
do {
acceptConnections();
} while (true);
} catch (IOException e) {
printStatusMessage("ERROR ! Socket is already open or inaccessible (" + PORT + ")");
}
}
private void stopServer() {
printStatusMessage("Termin ating 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,t his);
clientHandlers.add(handler );
pool.execute(handler);
} catch(IOException ioe) {
printStatusMessage(ioe.get Message()) ;
}
}
public void printStatusMessage(String message) {
// ERROR NullPointerException ...
this.statusWindow.setText( statusWind ow.getText ()+message +"\n");
}
public void printConvoMessage(String message) {
this.convoWindow.setText(c onvoWindow .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.newCachedThreadP ool();
guiThread = new Thread(new Runnable() {
public void run() {
initComponents();
}
});
guiThread.start();
server = new RiotServer();
server.setStatusWindow(txt Status);
server.setConvoWindow(txtC onvo);
}
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.newCachedThreadP
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("Starti
try {
sock = new ServerSocket(PORT);
printStatusMessage("SUCCES
do {
acceptConnections();
} while (true);
} catch (IOException e) {
printStatusMessage("ERROR ! Socket is already open or inaccessible (" + PORT + ")");
}
}
private void stopServer() {
printStatusMessage("Termin
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,t
clientHandlers.add(handler
pool.execute(handler);
} catch(IOException ioe) {
printStatusMessage(ioe.get
}
}
public void printStatusMessage(String message) {
// ERROR NullPointerException ...
this.statusWindow.setText(
}
public void printConvoMessage(String message) {
this.convoWindow.setText(c
}
public void echoToClients(String s){
for (RiotClientHandler clh : clientHandlers) {
clh.echoToClient(s);
}
}
}
In the GUI I have this:
public RiotServerGUI() {
pool = Executors.newCachedThreadP
guiThread = new Thread(new Runnable() {
public void run() {
initComponents();
}
});
guiThread.start();
server = new RiotServer();
server.setStatusWindow(txt
server.setConvoWindow(txtC
}
Thats because you call them beofre statusWindow has been set.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
Perhaps pass the window in the ctor.
ASKER
Found it. I'm using guiThread.start() ... replaced it with run().
ASKER
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)
That doesn't start a new thread, it calls the run() method of your thread instance (on the same thread)
stop() is also deprectaed and its use should be avoided.