【问题标题】:Multiple threads communicating with Pipe streams and using PushbackInputStream多个线程与 Pipe 流通信并使用 PushbackInputStream
【发布时间】:2012-11-03 22:32:05
【问题描述】:

问题如下:

创建 3 个线程。

  • 第一个会生成10个随机数,
  • 第二个将把这 10 个的偶数相加。
  • 第三个将把相同的 10 个随机数的奇数相加。

我的问题是:在第二个线程中,我读取了所有数字并将它们推回流中,但是当第三个线程想要从流中读取时,读取的第一个值是 -1 ?!

代码如下:

//main program 
import java.io.*;
public class anonymous {

public static void main(String[] args) throws  IOException, InterruptedException {
    final PipedOutputStream out= new PipedOutputStream();  
    final PipedInputStream in= new PipedInputStream(out);
    Thread1 th1 = new Thread1(out);
    Thread2 th2 = new Thread2(in);
    Thread3 th3 = new Thread3(in); 
    Thread t1 = new Thread(th1);
    Thread t2 = new Thread(th2); 
    Thread t3 = new Thread(th3); 
    t1.start(); 
    t2.start();
    t2.join();
    t3.start(); 
    t3.join();
    System.out.println("main finished.");   
}
}

//Thread1
import java.io.*;
import java.util.Random;
public class Thread1 implements Runnable{
PipedOutputStream out=null;
Random r = new Random();
public Thread1(PipedOutputStream send){
    this.out = send; 
}
public void run(){
    int num;
    System.out.println("thread 1 generated random numbers: ");
    try{
    for ( int i=0; i<10; i++)
        {
        num=r.nextInt(10);
        System.out.print(num + " ");
        out.write(num);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } 
try {
    out.close();
} catch (IOException e) {
    e.printStackTrace();
}
System.out.println("\nthread 1 finished");
}
}

//Thread2
import java.io.*;
public class Thread2 implements Runnable{
PipedInputStream in=null;
public Thread2( PipedInputStream get){
    this.in = get;

}
public void run(){ 
    PushbackInputStream push = new PushbackInputStream(in , 10);
    //PushbackInputStream takes an InputStream and it will read the first 10 bytes
            // in the stream and push them back to the stream
    try {
        byte[] byteArr = new byte[10]; 
        int i, sum=0, idx=0;
        i=push.read();
        while (i != -1)
        { 
            if( i%2 == 0)
                sum += i; 
            byteArr[idx]=( byte) i;
            i=push.read();
            idx++;
        }
        push.unread(byteArr,0 , 10);
        System.out.println("thread 2: the sum of even random numbers: " + sum); 
    } catch (IOException e) {
        e.printStackTrace();
    }
System.out.println("thread 2 finished"); 
}
}

//Thread3
import java.io.*;
public class Thread3 implements Runnable{

PipedInputStream in;
public Thread3( PipedInputStream get){
    this.in = get;
}
public void run(){
    try {
        int i, sum=0;
        i=in.read();
        while (i != -1)
        { 
            if( i%2 == 1)
                sum += i; 
            i=in.read();
        }
        System.out.println("thread 3: the sum of odd random numbers:  " + sum);
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        in.close();
    } catch (IOException e){
        e.printStackTrace();
    } 
System.out.println("thread 3 finished"); 
}
}

输出如下:

线程1生成随机数:
4 8 7 5 6 8 7 1 0 5
线程 1 完成
线程 2:偶数随机数之和:26
线程 2 完成
线程3:奇数随机数之和:0
线程 3 完成
主要完成。

【问题讨论】:

  • 为什么不使用两个管道(一个用于偶数,一个用于奇数)并让线程 1 为每个数字决定将其写入哪个管道?
  • 是的,这是可能的解决方案,谢谢。但很好奇是否可以使用 pushbackinputstream 解决,程序中的错误是什么?
  • 你应该使用新的 PushBackInputStream(PipedInputStream p ) 构造函数并使用 pushbackInputStream 的引用调用 unread 以获得所需的结果。

标签: java multithreading random iostream pipe


【解决方案1】:

您应该在第二个和第三个流之间使用另一个管道,而不是滥用推回功能。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-06
    • 1970-01-01
    • 2015-10-26
    • 1970-01-01
    • 2023-03-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多