如何使用CountDownlatch使线程按顺序工作?

时间:2015-04-03 08:07:57

标签: java multithreading countdownlatch cyclicbarrier

我正在学习如何在java中使用countdownLatch,我在代码中创建了一个如下所示的简单示例。

我学到的机制是,它只是强制一个线程等待其他人直到完成工作的方式,然后等待的线程将在其他线程时启动它( s)完成。

我的问题是,如果我有4个线程't1,t2,t3和t4',它们应按照所述顺序开始,并且每个线程应该在前导/前一个完成时开始。换句话说,t2应该等待t1并且在t1结束时开始,并且t3应该等待t2并且在t2结束时开始,t4应该等待t3并且在t3结束时开始。

1 - 如何使用CountDownLatch和循环屏障?

2-是传递给CountDownLatch类的构造函数的countDown参数应该表示等待的线程数?

public class MainClass {

public static void main(String[] args) {

    CountDownLatch latch1 = new CountDownLatch(1);
    Thread t1 = new Thread(new AscendingOrder(latch1));
    Thread t2 = new Thread(new DescendingOrder(latch1));

    t1.start();
    t2.start();
}

static class AscendingOrder implements Runnable {

    private CountDownLatch latch;

    public AscendingOrder(CountDownLatch latch) {
        // TODO Auto-generated constructor stub
        this.latch = latch;
    }

    public void run() {
        // TODO Auto-generated method stub
        System.out.println("thread t1 started.");

        for (int i = 0; i < 10; i++)
            System.out.println(i); 

        this.latch.countDown();
    }

}

static class DescendingOrder implements Runnable {

    private CountDownLatch latch;

    public DescendingOrder(CountDownLatch latch1) {
        // TODO Auto-generated constructor stub
        this.latch = latch1;
    }

    public void run() {
        // TODO Auto-generated method stub
        System.out.println("thread t2 started and waiting");

        try {
            this.latch.await();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        for (int i = 10; i > 0; i--)
            System.out.println(i); 
    }

}

}

1 个答案:

答案 0 :(得分:0)

  

1 - 如何使用CountDownLatch和循环屏障?

CyclicBarrier不是这个问题的理想选择,我写过帖子比较两者请看看here

public class CountDownLatchTest {

    public static void main(String[] args) {
        CountDownLatch first = new CountDownLatch(1);
        CountDownLatch prev = first;
        for(int i=0;i<10;i++) {
            CountDownLatch next = new CountDownLatch(1);
            new TestThread(prev, next, i).start();
            prev = next;
        }       
        first.countDown();

    }

    public static class TestThread extends Thread {

        private CountDownLatch prev;
        private CountDownLatch next;
        private int id;

        public TestThread(CountDownLatch prev, CountDownLatch next, int id) {
            this.prev = prev;
            this.next = next;
            this.id = id;
        }

        public void run() {
            if (prev != null) {
                try {
                    prev.await();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            try {
                doWork();
            } finally {
                if (next != null) {
                    next.countDown();
                }
            }
        }

        public void doWork() {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            System.out.println("Work done in Thread : "
                    + id);
        }

    }
}
  

2-是传递给构造函数的countDown参数   CountDownLatch类应该表示等待的线程数?

CountDownLatch.await()将一直等到CountDownLatch.countDown()方法被称为countDown参数次。