package three.day.io;
import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
public class PipedStreamTs {
public static void main(String[] args){
Sender sender = new Sender();
Reciever reciever = new Reciever();
try {
sender.getPipedOutputStream().connect(reciever.getPipedInputStream());
} catch (IOException e) {
e.printStackTrace();
}
sender.start();
reciever.start();
}
}
class Sender extends Thread{
private PipedOutputStream outStream;
private ThreadLocal<PipedOutputStream> threadLocal = new ThreadLocal<PipedOutputStream>();
public Sender(){
}
public PipedOutputStream getPipedOutputStream(){
outStream = threadLocal.get();
if(outStream == null){
outStream = new PipedOutputStream();
threadLocal.set(outStream);
}
return outStream;
}
public void run(){
if(outStream != null){
String msg = "你收到了吗?";
try {
outStream.write(msg.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
finally{
try {
outStream.close();
outStream = null;
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
class Reciever extends Thread{
private PipedInputStream inStream;
private ThreadLocal<PipedInputStream> threadLocal = new ThreadLocal<PipedInputStream>();
public PipedInputStream getPipedInputStream(){
inStream = threadLocal.get();
if(inStream == null){
inStream = new PipedInputStream();
threadLocal.set(inStream);
}
return inStream;
}
public void run(){
if(inStream != null){
try {
byte[] b = new byte[1024];
int len = inStream.read(b);
System.out.println(new String(b, 0, len));
} catch (IOException e) {
e.printStackTrace();
}
finally{
try {
inStream.close();
inStream = null;
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
public class PipedStreamTs {
public static void main(String[] args){
Sender sender = new Sender();
Reciever reciever = new Reciever();
try {
sender.getPipedOutputStream().connect(reciever.getPipedInputStream());
} catch (IOException e) {
e.printStackTrace();
}
sender.start();
reciever.start();
}
}
class Sender extends Thread{
private PipedOutputStream outStream;
private ThreadLocal<PipedOutputStream> threadLocal = new ThreadLocal<PipedOutputStream>();
public Sender(){
}
public PipedOutputStream getPipedOutputStream(){
outStream = threadLocal.get();
if(outStream == null){
outStream = new PipedOutputStream();
threadLocal.set(outStream);
}
return outStream;
}
public void run(){
if(outStream != null){
String msg = "你收到了吗?";
try {
outStream.write(msg.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
finally{
try {
outStream.close();
outStream = null;
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
class Reciever extends Thread{
private PipedInputStream inStream;
private ThreadLocal<PipedInputStream> threadLocal = new ThreadLocal<PipedInputStream>();
public PipedInputStream getPipedInputStream(){
inStream = threadLocal.get();
if(inStream == null){
inStream = new PipedInputStream();
threadLocal.set(inStream);
}
return inStream;
}
public void run(){
if(inStream != null){
try {
byte[] b = new byte[1024];
int len = inStream.read(b);
System.out.println(new String(b, 0, len));
} catch (IOException e) {
e.printStackTrace();
}
finally{
try {
inStream.close();
inStream = null;
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}