zoukankan      html  css  js  c++  java
  • java CyclicBarrier 2

    //Listing 6-2. Using a Cyclic Barrier to Decompose a Task into Subtasks
    import java.util.concurrent.BrokenBarrierException;
    import java.util.concurrent.CyclicBarrier;
    
    public class T {
        public static void main(String[] args) {
            float[][] matrix = new float[3][3];
            int counter = 0;
            for (int row = 0; row < matrix.length; row++)
                for (int col = 0; col < matrix[0].length; col++)
                    matrix[row][col] = counter++;
            dump(matrix);
            System.out.println();
            Solver solver = new Solver(matrix);
            System.out.println();
            dump(matrix);
        }
    
        static void dump(float[][] matrix) {
            for (int row = 0; row < matrix.length; row++) {
                for (int col = 0; col < matrix[0].length; col++)
                    System.out.print(matrix[row][col] + " ");
                System.out.println();
            }
        }
    }
    
    class Solver {
        final int N;
        final float[][] data;
        final CyclicBarrier barrier;
    
        class Worker implements Runnable {
            int myRow;
            boolean done = false;
    
            Worker(int row) {
                myRow = row;
            }
    
            boolean done() {
                return done;
            }
    
            void processRow(int myRow) {
                System.out.println("Processing row: " + myRow);
                for (int i = 0; i < N; i++)
                    data[myRow][i] *= 10;
                done = true;
            }
    
            @Override
            public void run() {
                while (!done()) {
                    processRow(myRow);
                    try {
                        barrier.await();
                    } catch (InterruptedException ie) {
                        return;
                    } catch (BrokenBarrierException bbe) {
                        return;
                    }
                }
            }
        }
    
        public Solver(float[][] matrix) {
            data = matrix;
            N = matrix.length;
            barrier = new CyclicBarrier(N, new Runnable() {
                @Override
                public void run() {
                    mergeRows();
                }
            });
            for (int i = 0; i < N; ++i)
                new Thread(new Worker(i)).start();
            waitUntilDone();
        }
    
        void mergeRows() {
            System.out.println("merging");
            synchronized ("abc") {
                "abc".notify();
            }
        }
    
        void waitUntilDone() {
            synchronized ("abc") {
                try {
                    System.out.println("main thread waiting");
                    "abc".wait();
                    System.out.println("main thread notified");
                } catch (InterruptedException ie) {
                    System.out.println("main thread interrupted");
                }
            }
        }
    }
  • 相关阅读:
    css 网格布局简单应用
    服务器常用状态码
    js 操作数组
    下拉框 tree 基于 EasyUi
    c# 导出Excel
    select 多选 (EasyUI)
    如何提高sql查询速度
    总结JS中string、math、array的常用的方法
    php 验证邮箱 php验证手机号码 ph验证身份证
    jquery+php ajax上传多图片即时显示
  • 原文地址:https://www.cnblogs.com/rojas/p/5367318.html
Copyright © 2011-2022 走看看