线程管道的一个实验
我一直在想,如果在线程管道中,输入的速度比输出的速度快很多的话会出现什么情况,或者反过来又是怎样写了个实验程序,发现不管是哪种情况,最后都将快的变慢。当输入快时,会抑制输入线程;输出快时,会抑制输出线程
<div style="">http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifpackagecom.threadlesson01.study;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifimportjava.io.DataOutputStream;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifimportjava.io.IOException;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifimportjava.io.PipedOutputStream;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gifpublicclassSendDataThreadextendsThread...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifprivateDataOutputStreamdos=null;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif/***//**
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif*@paramdos
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif*/
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gifpublicSendDataThread(PipedOutputStreampos)...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifthis.dos=newDataOutputStream(pos);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif}
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif@Override
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gifpublicvoidrun()...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifintindex=0;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gifwhile(true)...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.giftry...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifindex++;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifsleep(1000);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifdoublerand=Math.random();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifdos.writeDouble(rand);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifSystem.out.println("Thisisinthe"+index+"SendThread:"+rand);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif}catch(InterruptedExceptione)...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif//TODOAuto-generatedcatchblock
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gife.printStackTrace();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif}catch(IOExceptione)...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif//TODOAuto-generatedcatchblock
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gife.printStackTrace();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif}
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif}
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif}
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif}
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
页:
[1]