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

我有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++;
.....

最佳答案

一种可能也简化结构的选项是使用BlockingQueue


main中,创建两个队列,每个队列分别传递给每个生产者,两个消费者
生产者写他们的队列
使用者从两个队列中读取并执行其操作:


while (true) {
  int[] matrix1 = queue1.remove();
  int[] matrix2 = queue2.remove();
  // process the two
}

10-07 12:30