线程管道的一个实验

我一直在想,如果在线程管道中,输入的速度比输出的速度快很多的话会出现什么情况,或者反过来又是怎样
写了个实验程序,发现不管是哪种情况,最后都将快的变慢。当输入快时,会抑制输入线程;输出快时,会抑制输出线程
packagecom.threadlesson01.study;

importjava.io.DataOutputStream;
importjava.io.IOException;
importjava.io.PipedOutputStream;

publicclassSendDataThreadextendsThread...{
privateDataOutputStreamdos=null;

/***//**
*
@paramdos
*/

publicSendDataThread(PipedOutputStreampos)...{
this.dos=newDataOutputStream(pos);
}


@Override
publicvoidrun()...{
intindex=0;
while(true)...{
try...{
index
++;
sleep(
1000);
doublerand=Math.random();
dos.writeDouble(rand);
System.out.println(
"Thisisinthe"+index+"SendThread:"+rand);
}
catch(InterruptedExceptione)...{
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
catch(IOExceptione)...{
//TODOAuto-generatedcatchblock
e.printStackTrace();
}

}

}


}

packagecom.threadlesson01.study;


importjava.io.*;

publicclassRecieveDataThreadextendsThread...{
privateDataInputStreamdis=null;

/***//**
*
@paramdis
*/

publicRecieveDataThread(PipedInputStreampis)...{
this.dis=newDataInputStream(pis);
}


@Override
publicvoidrun()...{
//TODOAuto-generatedmethodstub
intindex=0;
while(true)...{
try...{
index
++;
sleep(
100);
System.out.println(
"The"+index+"ReadfromPipedInputStream:"+dis.readDouble());
}
catch(InterruptedExceptione)...{
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
catch(IOExceptione)...{
//TODOAuto-generatedcatchblock
e.printStackTrace();
}

}

}

}

packagecom.threadlesson01.study;

importjava.io.IOException;
importjava.io.PipedInputStream;
importjava.io.PipedOutputStream;

publicclassTestMain...{
publicstaticvoidmain(Stringarg[])...{

try...{
PipedOutputStreampos
=newPipedOutputStream();
PipedInputStreampis
=newPipedInputStream(pos);
SendDataThreadsdt
=newSendDataThread(pos);
RecieveDataThreadrdt
=newRecieveDataThread(pis);
sdt.start();
rdt.start();
}
catch(IOExceptione)...{
//TODOAuto-generatedcatchblock
e.printStackTrace();
}

}

}

当输出比输入快时,Result:
This is in the 1 Send Thread: 0.16151098740750125
The 1 Read from PipedInputStream: 0.16151098740750125
This is in the 2 Send Thread: 0.7120194002253469
The 2 Read from PipedInputStream: 0.7120194002253469
This is in the 3 Send Thread: 0.8528097261914339
The 3 Read from PipedInputStream: 0.8528097261914339
This is in the 4 Send Thread: 0.9109161534568372
The 4 Read from PipedInputStream: 0.9109161534568372
This is in the 5 Send Thread: 0.7768039908168954
The 5 Read from PipedInputStream: 0.7768039908168954
This is in the 6 Send Thread: 0.8719109921893281
The 6 Read from PipedInputStream: 0.8719109921893281
This is in the 7 Send Thread: 0.7247112439236129
The 7 Read from PipedInputStream: 0.7247112439236129
This is in the 8 Send Thread: 0.22120976865470565
The 8 Read from PipedInputStream: 0.22120976865470565
当输入比输出快时,Result:
……
The 13 Read from PipedInputStream: 0.2315708377615764
This is in the 129 Send Thread: 0.23167266261217678
This is in the 130 Send Thread: 0.5757523398415085
This is in the 131 Send Thread: 0.9662210303251764
This is in the 132 Send Thread: 0.9538605084985049
This is in the 133 Send Thread: 0.8889780948652806
This is in the 134 Send Thread: 0.7419512492943433
This is in the 135 Send Thread: 0.3622398890338431
This is in the 136 Send Thread: 0.253115630971971
This is in the 137 Send Thread: 0.4309240925796164
This is in the 138 Send Thread: 0.6814706087861843
The 14 Read from PipedInputStream: 0.7872902847515667
This is in the 139 Send Thread: 0.15388195849710673
This is in the 140 Send Thread: 0.43686862090227363
This is in the 141 Send Thread: 0.4155404423497907
This is in the 142 Send Thread: 0.2789332111567
The 15 Read from PipedInputStream: 0.5652592690681899
This is in the 143 Send Thread: 0.41013128238855456
The 16 Read from PipedInputStream: 0.6409729371490666
This is in the 144 Send Thread: 0.11322627532609841
The 17 Read from PipedInputStream: 0.6281227248523248
This is in the 145 Send Thread: 0.6754434599524916
The 18 Read from PipedInputStream: 0.2433345275064387
This is in the 146 Send Thread: 0.9870542183973696
The 19 Read from PipedInputStream: 0.1706111109960894
This is in the 147 Send Thread: 0.6330426569615286
The 20 Read from PipedInputStream: 0.9658561550134379
对于线程管道,里面应该存在一个缓冲器,在java source中可以找到
private static final int DEFAULT_PIPE_SIZE = 1024;
buffer = new byte[pipeSize];
PipedInputStream.java
privatevoidawaitSpace()throwsIOException...{
while(in==out)...{
checkStateForReceive();

/**//*full:kickanywaitingreaders*/
notifyAll();
try...{
wait(
1000);
}
catch(InterruptedExceptionex)...{
thrownewjava.io.InterruptedIOException();
}

}

}
当PipedInputStream的存储慢了以后,会等候1s
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值