This page looks best with JavaScript enabled

BlockingQueue-CountDownLatch

 ·  ☕ 2 min read
  1. BlockingQueue:Java提供的线程安全的队列接口。
  2. CountDownWatch:可用来让一个线程一直等待watch的值为0,另一个线程来修改watch的值。达到让线程依次执行的目的。

BlockingQueue的常用实现类:

  • ArrayBlockingQueue
  • DelayQueue
  • LinkedBlockingDeque

操作方法:

操作\结果 Throw Exception Return false Block Times Out
插入 add(E e) offer(E e) put(E e) offer(E e,int timeOut,TimeUtil util)
检索并删除 remove() poll() take() poll(long timeout, TimeUnit unit)
检索不删除 element() peek()

当对队列的操作,被打断或者队列容量填满时,以上方法的返回如上表不同。有的会抛异常,有的会返回false,有的会一直阻塞进程等待。

BlockingQueue实例,生产者-消费者问题:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
class Producer implements Runnable {
    private final BlockingQueue<Integer> queue;
    Producer(BlockingQueue<Integer> queue) {
        this.queue = queue;
    }
    @Override
    public void run() {
        System.out.println("start produce");
        while (true) {
            boolean insertSuc = queue.offer(producer());
            if (!insertSuc) {
                System.out.println("produce over");
                return;
            }
        }
    }
    int i = 0;
    private Integer producer() {
        return ++i;
    }
}
class Consumer implements Runnable {
    private final BlockingQueue<Integer> queue;
    Consumer(BlockingQueue<Integer> queue) {
        this.queue = queue;
    }
    @Override
    public void run() {
        System.out.println("start consumer");
        while (true) {
            try {
                Integer i = queue.take();
                System.out.print( i+" ");
            } catch (InterruptedException e) {           
            }
        }
    }
}
BlockingQueue<Integer> queue = new ArrayBlockingQueue<>(10);
new Thread(new Producer(queue)).start();
new Thread(new Consumer(queue)).start();

输出:

1
2
3
4
start produce
produce over
start consumer
1 2 3 4 5 6 7 8 9 10

CountDownWatch实例:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
CountDownLatch countDownLatch = new CountDownLatch(10);
Runnable r1 = new Runnable() {
    @Override
    public void run() {
        try {
            System.out.println("r1 wait countDown count=" + countDownLatch.getCount());
            countDownLatch.await();
            System.out.println("r1 goon countDown count=" + countDownLatch.getCount());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
};
Runnable r2 = new Runnable() {
    @Override
    public void run() {
        int i = 10;
        System.out.println("r2 run");
        while (i > 0) {
            countDownLatch.countDown();
            System.out.print(countDownLatch.getCount() + " ");
            i--;
        }
    }
};
new Thread(r1).start();
new Thread(r2).start();

输出:当countDown的值为0时,thread1就从等待变为运行。

1
2
3
4
r1 wait countDown count=10
r2 run
9 8 7 6 5 4 3 2 1 r1 goon countDown count=0
0
Support the author with
alipay QR Code
wechat QR Code

Yang
WRITTEN BY
Yang
Developer