At some point in my server application, I want to stop some threads that perform I / O blocking operations.
For example, one of them has the following run() method:
public void run() { System.out.println("GWsocket thread running"); int len; byte [] buffer = new byte[1500]; try { this.in = new DataInputStream(this.socket.getInputStream()); this.out = new DataOutputStream(this.socket.getOutputStream()); running = true; while (running){ len = in.read (buffer); if (len < 0) running = false; else parsepacket (buffer, len); } }catch (IOException ex) { System.out.println("GWsocket catch IOException: "+ex); }finally{ try { System.out.println("Closing GWsocket"); fireSocketClosure(); in.close(); out.close(); socket.close(); }catch (IOException ex) { System.out.println("GWsocket finally IOException: "+ex); } } }
If I want to stop the thread executing this code, what should I do?
Here they show how to do it ( How to stop a thread that is waiting for long periods of time (for example, for input)? ), But I do not understand what they mean:
For this method to work, it is important that any method that catches an interrupt exception and is not ready for an immediate solution repeats the exception. We say repeating, not repeating, because it is not always possible to rebuild an exception. If the method that catches the thrown exception does not declare this (checked) exception, then it must "relive itself" using the after-spell: Thread.currentThread (). interrupt ();
Can someone tell me? Some code examples would be greatly appreciated.
source share