PipedInputStream和Out解决生产者-消费者问题
class sender extends Thread { PipedOutputStream out = new PipedOutputStream(); public PipedOutputStream getOut() { return out; } public void run() { String str = "Hello,receiver ! I`m sender\n "; try { out.write(str.getBytes()); out.close(); } catch(Exception e) { e.printStackTrace(); } } } //receiver.java class receiver extends Thread { PipedInputStream in = new PipedInputStream(); public PipedInputStream getIn() { return in; } public void run() { byte [] buf = new byte[1024]; try { int len = in.read(buf); System.out.println( "the following is from sender:\n "+new String(buf,0,len)); in.close(); }catch(Exception e) { e.printStackTrace(); } } } //TestPiped.java class TestPiped { public static void main(String [] args) { sender s = new sender(); receiver r = new receiver(); PipedOutputStream out = s.getOut(); PipedInputStream in = r.getIn(); try { in.connect(out); s.start(); r.start(); } catch(Exception e) { e.printStackTrace(); } } }