代码
public static void main(String
[] args
) throws Exception
{
ExecutorService nFixed
= Executors
.newFixedThreadPool(2);
PipedInputStream pi
= new PipedInputStream();
PipedOutputStream po
= new PipedOutputStream();
pi
.connect(po
);
nFixed
.execute(new Runnable() {
@Override
public void run() {
try {
int sum
= 0;
byte[] bytes
= new byte[100];
int length
=0;
for (int i
= 1; i
<=100; i
++) {
Thread
.sleep(2000);
sum
+=i
;
po
.write(sum
);
System
.out
.println("写入了"+sum
);
}
} catch (Exception e
) {
e
.printStackTrace();
}
}
});
nFixed
.execute(new Runnable() {
@Override
public void run() {
try {
int length
=0;
while((length
=pi
.read())!=-1) {
System
.out
.println(length
);
}
} catch (Exception e
) {
e
.printStackTrace();
}
}
});
}
转载请注明原文地址:https://tech.qufami.com/read-4522.html