multithreading - Thread interrupt() not interrupting the thread in java (UDP Socket) -


this question has been asked before here.i tried figure out problem learning them still can't find solution it.i posting minimal code interrupt problem.i missing silly guys might able with.

import java.io.ioexception; import java.net.datagrampacket; import java.net.inetaddress; import java.net.multicastsocket; import java.net.unknownhostexception;  public class optitrack implements runnable { private multicastsocket s;  public optitrack(multicastsocket socket) {     // socket acquired parent     this.s = socket; }  public void start_track() {     inetaddress group;     try {         group = inetaddress.getbyname("xx.xx.xx.xx");         s = new multicastsocket(1511);         s.joingroup(group);     } catch (unknownhostexception e1) {         // todo auto-generated catch block         e1.printstacktrace();     } catch (ioexception e1) {         // todo auto-generated catch block         e1.printstacktrace();     }     system.out.println("have joined multicast group?");     while (true) {          // responses!         if (thread.currentthread().isinterrupted()) {             thread.currentthread().interrupt();             return;         }         datagrampacket recv = new datagrampacket(buf, buf.length);         try {             s.receive(recv);         } catch (ioexception e) {             // todo auto-generated catch block             e.printstacktrace();         }          // calculations , functions here not important         // problem     } }  @override public void run() {     // todo auto-generated method stub     start_track(); }  } 

also here code snippet call interrupt parent thread.

if(track.isalive())                 {                     //if(socket!=null)                     //{                         socket.close();                     //}                     track.interrupt();                     //track.stop();                 } 

track initialised

tracker=new optitrack(socket); track=new thread(tracker); track.start(); 

i trying close socket interrupt receive function , interrupt thread itself.i tried without interrupting receive function , directly calling thread.interrupt above code same results.

it of great if can figure out what's wrong or correct way is.

if interrupt thread, have check in thread , perform exiting logic - interrupting no "black magic" canceling interrupted thread.

interrupting blocking reading on socket done closing socket combined try-catch-block spanning reading , reacting closed socket exiting orderly.


Comments

Popular posts from this blog

google api - Incomplete response from Gmail API threads.list -

Installing Android SQLite Asset Helper -

Qt Creator - Searching files with Locator including folder -