线程不可中断阻塞的处理JAVA

//改写interrupt操作
public class ReaderThread extends Thread{
    
    
  private final Socket socket;
  private final InputStream in;

  public ReaderThread() throws IOException{
    
    
    this.socket = socket;
    this.in = socket.getInputStream();
  }

  public void interrupt(){
    
    
    try{
    
    
      socket.close();
    }catch(IOException ignored){
    
    
      
    }finally{
    
     
      super.interrupt();
    }
  }

  public void run(){
    
    
    try{
    
    
      byte[] buf = new byte[BUFSZ];
      while(true){
    
    
        int count = in.read(buf);
        if(count < 0)
          break;
        else if(count < 0){
    
    
          processBuffer(buf,count);
        }
      }
    }catch(IOException e){
    
    
      /* 允许线程退出 */
    }

  }

}

猜你喜欢

转载自blog.csdn.net/weixin_37632716/article/details/118444126