Threads and Interrupt();

Asked

Viewed 200 times

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. Use Thread t = new Thread(servidor); t.start(); and to interrupt t.interrupt();

  • But the interrupt function works normally, there is no need for complements?

  • 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

  • Do not need parameters, you can also use Thread.currentThread().interrupt();

  • 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

  • It does not finish the thread, websocket remains open and running normally. @Valdeirpsr

Show 1 more comment
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.