线程管道的一个实验

本文通过实验探讨了Java中线程管道的工作原理及其内部缓冲机制。当输入速度与输出速度不匹配时,线程管道如何调整以维持数据流的稳定。通过分析源码揭示了其背后的设计理念。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

import java.io.DataOutputStream;
import java.io.IOException;
import java.io.PipedOutputStream;

public class SendDataThread extends Thread {
    
private DataOutputStream dos = null ;
    
    
/**
     * 
@param dos
     
*/

    
public SendDataThread(PipedOutputStream pos) {
        
this.dos = new DataOutputStream(pos);
    }


    @Override
    
public void run() {
        
int index = 0 ;
        
while(true){
            
try {
                index 
++ ;
                sleep(
1000);
                
double rand = Math.random();
                dos.writeDouble(rand);
                System.out.println(
"This is in the " + index + " Send Thread: " + rand);
            }
 catch (InterruptedException e) {
                
// TODO Auto-generated catch block
                e.printStackTrace();
            }
 catch (IOException e) {
                
// TODO Auto-generated catch block
                e.printStackTrace();
            }
            
        }

    }

    
}

package com.threadlesson01.study;


import java.io.*;

public class RecieveDataThread extends Thread {
    
private DataInputStream dis = null ;

    
/**
     * 
@param dis
     
*/

    
public RecieveDataThread(PipedInputStream pis) {
        
this.dis = new DataInputStream(pis);
    }


    @Override
    
public void run() {
        
// TODO Auto-generated method stub
        int index = 0 ;
        
while(true){
            
try {
                index
++;
                sleep(
100);
                System.out.println(
"The " + index + " Read from PipedInputStream: " + dis.readDouble());
            }
 catch (InterruptedException e) {
                
// TODO Auto-generated catch block
                e.printStackTrace();
            }
 catch (IOException e) {
                
// TODO Auto-generated catch block
                e.printStackTrace();
            }

        }

    }

}

package com.threadlesson01.study;

import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;

public class TestMain {
    
public static void main(String arg[]){

        try {
            PipedOutputStream pos 
= new PipedOutputStream();            
            PipedInputStream pis 
= new PipedInputStream(pos);
            SendDataThread sdt 
= new SendDataThread(pos);
            RecieveDataThread rdt 
= new RecieveDataThread(pis);
            sdt.start();
            rdt.start();
        }
 catch (IOException e) {
            
// TODO Auto-generated catch block
            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
    private void awaitSpace() throws IOException {
    
while (in == out) {
        checkStateForReceive();

        
/* full: kick any waiting readers */
        notifyAll();
        
try {
            wait(
1000);
        }
 catch (InterruptedException ex) {
        
throw new java.io.InterruptedIOException();
        }

    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值