import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
/**
*
* @author Athena
*/
public class ThreadTest {
public static void main(String[] args) throws IOException {
A a = new A();
B b = new B();
PipedOutputStream out = a.getPipedOutputStream();
PipedInputStream in = b.getPipedInputStream();
out.connect(in);
a.start();
b.start();
}
}
class A extends Thread {
PipedOutputStream out = new PipedOutputStream();
public PipedOutputStream getPipedOutputStream() {
return out;
}
@Override
public void run() {
try {
out.write("你好!!".getBytes());
out.flush();
System.out.println("A class is close");
} catch (IOException ex) {
}
}
}
class B extends Thread {
PipedInputStream in = new PipedInputStream();
public PipedInputStream getPipedInputStream() {
return in;
}
@Override
public void run() {
try {
byte[] buffer = new byte[1024];
int read = in.read(buffer);
System.out.println(new String(buffer, 0, read));
System.out.println("B class is close : " + read);
} catch (IOException ex) {
}
}
}
全站熱搜
留言列表