1
I wonder how I can pause my Thread efficiently and correctly with the interrupt method(); I’m with a part of my class that I started, but I’d like to pause it.
My project consists of a websocket, that I would like to stop, but I did not succeed in my research, so I decided to put it within a thread and pause that way, I’d like a little help.
Follows the code down:
public Runnable servidor = new Runnable() {
public void run() {
try {
new ServidorExt(8080).start();
} catch (UnknownHostException ex) {
Logger.getLogger(painelUsuarios.class.getName()).log(Level.SEVERE, null, ex);
}
}
};
I’m starting with command " new Thread(server). start(); " and trying to stop the same by " new Thread(server). Interrupt(); ".
You’re creating a new
thread
, so you’re not getting it. UseThread t = new Thread(servidor); t.start();
and to interruptt.interrupt();
– Valdeir Psr
But the interrupt function works normally, there is no need for complements?
– neto schneider
For example, I used the example you gave, but the class does not stop, so much so that my websocket remains open in the same way. @Valdeirpsr
– neto schneider
Do not need parameters, you can also use
Thread.currentThread().interrupt();
– Valdeir Psr
I tried to run the 2 methods at once, starting and finishing right away, but I get an error: "Exception in thread "main" java.lang.Illegalthreadstateexception " @Valdeirpsr
– neto schneider
It does not finish the thread, websocket remains open and running normally. @Valdeirpsr
– neto schneider