How to interrupt a thread blocked by some socket IO operations without closing

I really want to know how to interrupt a thread blocked by some I / O operation? I / O is through socket, I don't want to close () socket

I try to use thread Interrupt() to interrupt the thread, but failed

Thread blocked by IO operation:

void run(){
    byte[] data=new byte[1024];
    in.read(data);//blocked!
}

Thread used to interrupt a blocked thread (not working):

void run(){
    blockedThread.interrupt();//no success!
}

Solution

When getting sockettimeoutexception, set a short read timeout on the socket and loop, and check thread every time isInterrupted(). Use thread Interrupt() to interrupt the thread

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>