日期:2014-05-20 浏览次数:21084 次
import java.io.*;
public class PipedStreamTest {
/**
*
* @param args
*
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Sender t1 = new Sender();
Receiver t2 = new Receiver();
PipedOutputStream os = t1.getOutput(); //The method getOutput() is undefined for the type Thread
PipedInputStream is = t2.getInput(); //The method getInput() is undefined for the type Thread
try {
is.connect(os);
} catch (IOException e) {
e.printStackTrace();
}
t1.start();
t2.start();
}
}
class Sender extends Thread {
private PipedOutputStream out = new PipedOutputStream();
public PipedOutputStream getOutput() {
return out;
}
public void run() {
try {
out.write("hello".getBytes());
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
class Receiver extends Thread {
private PipedInputStream in = new PipedInputStream();
public PipedInputStream getInput() {
return in;
}
public void run() {
byte[] buf = new byte[1024];
try {
int len = in.read(buf);
System.out.println("receive from sender:\n" + new String(buf, 0, len));
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
------解决方案--------------------
你定义的类型不对