1

在此处输入图像描述

你好 !我有一个小问题。我只是学习多线程,还不了解所有内容。

我有 3 个线程:1 和 2 随机生成一个矩阵,并且在每一步之后,它们通过管道矩阵元素发送类似的参数。3-d 线程比较它们并写入结果,然后第 1 步和第 2 步执行下一步,然后一次又一次地重复 5 次。

我的问题:我需要同步所有的踏板才能正常工作。这个问题与生产者/消费者问题有相似之处,但这里有 2 个生产者和 1 个消费者。


public class Producer1 extends Thread{
    CyclicBarrier cbar;
    public Producer1(CyclicBarrier c){
        cbar=c;
        new Thread(this).start();
    }
    private Random generator = new Random();
    int []matrix1 = new int[1000];


    private PipedWriter out = new PipedWriter();

    public PipedWriter getPipedWriter() {
        return out;
     }

    public void run() {
        for(int i =0;i<5;i++){
            matrix1[i]= generator.nextInt(10)+10;
             System.out.println("matrix1["+i+"]= "+matrix1[i]);
             try {

                  out.write(matrix1[i]);
                  cbar.await();
                  sleep(500);
                } catch (Exception e) {
                  throw new RuntimeException(e);
                }
        }
    }
}    

public class Producer2 extends Thread{
     Random generator = new Random();
     int []matrix2 = new int[1000];
     CyclicBarrier cbar;
        public Producer2(CyclicBarrier c){
            cbar=c;
            new Thread(this).start();
        }
     private PipedWriter out = new PipedWriter();

        public PipedWriter getPipedWriter() {
            return out;
         }
        public void run() {
            for(int i =0;i<5;i++){
                matrix2[i]= generator.nextInt(20)-10;
                 System.out.println("matrix2["+i+"]= "+matrix2[i]);
                 try {
                      out.write(matrix2[i]);
                      cbar.await();
                      sleep(500);
                    } catch (Exception e) {
                      throw new RuntimeException(e);
                    }
            }   
        }
} 

    public class Main {
    public static void main(String[] args) throws IOException { 
     Producer1 prod = new Producer1(new CyclicBarrier(2, new Consummer(prod,prod2)));   
        // here is a problem  "prod2 cannot be resolved to a variable" 
                                                     // How can i do it work??
     Producer2 prod2 = new Producer2(new CyclicBarrier(2, new Consummer(prod,prod2)));
         CyclicBarrier cb1 = new CyclicBarrier(2, new Consummer(prod,prod2)); 

        prod.start();
        prod2.start();
    }

}

public class Consummer extends Thread{
     private PipedReader el1;
     private PipedReader el2;


      public Consummer(Producer1 sender, Producer2 sender2) throws IOException {
        el1 = new PipedReader(sender.getPipedWriter());
        el2 = new PipedReader(sender2.getPipedWriter());
      }

    public void run() {
         try {

               while (true) {

                System.out.println("Element1 : " +  el1.read()+" Element2 : " +  el2.read());
               }
            } catch (IOException e) {
              throw new RuntimeException(e);
            }

    }

}


            for example : 
i=0
1-> 10
2-> 5
3-> first is bigger =10
i++;

i=1;
1-> 3
2-> 5
3-> Second is bigger =5
i++;

i=2;
1-> 4
2-> 4
3-> both are equal = 4
i++;
.....
4

2 回答 2

1

一个也可以简化结构的选项是使用BlockingQueue

  • main中,创建两个队列,将一个队列传递给每个生产者,并将两个队列传递给消费者
  • 生产者写入他们的队列
  • 消费者从两个队列中读取并做它的事情:

while (true) {
  int[] matrix1 = queue1.remove();
  int[] matrix2 = queue2.remove();
  // process the two 
}
于 2013-01-09T22:27:02.573 回答
0

传递barrier给消费者,让它在打印之前等待:

           while (true) {
            barrier.wait();
            System.out.println("Element1 : " +  el1.read()+" Element2 : " +  el2.read());
           }
于 2013-01-09T20:07:03.327 回答