2012-04-05 2 views

답변

3

내 코드에 java.io.IOException: Read end dead가 발생하여 그 원인을 발견했습니다. 아래 예제 코드 게시. 코드를 실행하면 "Read end dead"예외가 발생합니다. 면밀히 살펴보면 소비자 스레드는 스트림에서 "hello"를 읽고 종료합니다. 한편 프로듀서는 2 초 동안 잠들고 "세계"를 쓰려고하지만 실패합니다. 이와 관련된 문제는 여기에 설명 : http://techtavern.wordpress.com/2008/07/16/whats-this-ioexception-write-end-dead/

class ReadEnd { 
public static void main(String[] args) { 
    final PipedInputStream in = new PipedInputStream(); 
    new Thread(new Runnable() { //consumer 
     @Override 
     public void run() { 
      try { 
       byte[] tmp = new byte[1024]; 
       while (in.available() > 0) {   // only once... 
        int i = in.read(tmp, 0, 1024); 
        if (i < 0) 
         break; 
        System.out.print(new String(tmp, 0, i)); 
       } 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } finally { 

      } 
     } 
    }).start(); 
    PipedOutputStream out = null; 
    try { 

     out = new PipedOutputStream(in); 
     out.write("hello".getBytes()); 
     Thread.sleep(2 * 1000); 
     out.write(" world".getBytes()); //Exception thrown here 

    } catch (IOException e) { 
     e.printStackTrace(); 
    } catch (InterruptedException e) { 
     e.printStackTrace(); 
    } finally { 
    } 
} 

}