울타리 실례

1752 단어 자바
package cn.hc.sort.thread;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

/**
 *     
 * Created by yhc on 2015/5/6.
 */
public class CyclicBarrierTest {

    private static class Report implements Runnable{

        private final CyclicBarrier barrier;
        private final String provinceName;

        private Report(CyclicBarrier barrier, String provinceName) {
            this.barrier = barrier;
            this.provinceName = provinceName;
        }

        @Override
        public void run() {
            System.out.println("    "+provinceName+"  ,      ....");
            try {
                //TODO          
                Thread.sleep(2000);
                System.out.println(provinceName+"    ,    .");
                barrier.await();
                System.out.println("  "+provinceName+"           .");
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (BrokenBarrierException e) {
                e.printStackTrace();
            }

        }
    }

    public static void main(String[] args) {
        CyclicBarrier barrier=new CyclicBarrier(3);
        String [] provinceArray=new String[]{"   ","   ","   "};
        for (int i = 0; i < provinceArray.length; i++) {
            Thread thread=new Thread(new Report(barrier,provinceArray[i]));
            thread.start();
        }
    }


}


         ,      ....
         ,      ....
         ,      ....
       ,    .
       ,    .
       ,    .
                .
                .
                .

좋은 웹페이지 즐겨찾기