请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

Vector输出结果不是5000的问题

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
public class VectorExample {
 
    // 请求总数
    public static int clientTotal = 5000;
    // 并发数
    public static int threadTotal = 200;
     
    private static Vector<Integer> vector = new Vector<>();
     
    public static void main(String[] args) throws InterruptedException {
         
        ExecutorService exec = Executors.newCachedThreadPool();
        final Semaphore semaphore = new Semaphore(threadTotal);
        final CountDownLatch latch = new CountDownLatch(clientTotal);
        for (int i = 0; i < clientTotal; i++) {
            final int count = i;
            exec.execute(new Runnable() {
                 
                @Override
                public void run() {
                    try {
                        semaphore.acquire();
                        update(count);
                        semaphore.release();
                    catch (InterruptedException e) {
                        log.error("exception", e);
                    }
                }
            });
            latch.countDown();  // 每一个请求减一
        }
         
        latch.await(); // latch为0的时候输出结果
        exec.shutdown();
        log.info("size:{}",vector.size());
    }
     
    private static void update (int i) {
        vector.add(i);
    }

输出结果:

1
11:41:57.559 [main] INFO com.okali.concurrency.commonUnsafe.HashMapExample - size:4989


正在回答

插入代码

3回答

Jimin 2018-04-04 00:32:09
latch.countDown(); 位置错了~
0 回复 有任何疑惑可以回复我~
  • 老师,为什么countDown()在他截图的这个位置就会出问题?CountDownLatch 不是能够保证并发时的准确性的吗?
    回复 有任何疑惑可以回复我~ 2018-08-22 10:48:35
  • 这个要结合countdownlatch的原理来说,你记得对一下课程里那张图。每个线程执行完的时候要去调用countdown方法,达到-1的效果,这样线程都执行完时countdownlatch才能不再阻塞主线程。这是他的原理,必须按照原理去使用它的方法才能达到他本身的效果。
    回复 有任何疑惑可以回复我~ 2018-08-22 11:07:22
  • 您的意思是,如果放在外面,可能会发生重排序,先做了countdown,然后才执行execute。所以导致最后主线程执行的时候size < 5000?
    回复 有任何疑惑可以回复我~ 2018-08-22 13:47:40
贰零一贰 2018-12-31 18:47:15

我和楼主的问题一样,“countDownLatch.countDown();”的位置不对,写成下面这样就好了:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public static void main(String[] args) throws InterruptedException {
    ExecutorService executorService = Executors.newCachedThreadPool();
    final Semaphore semaphore = new Semaphore(threadTotal);
    final CountDownLatch countDownLatch = new CountDownLatch(clientTotal);
    for (int i = 0; i < clientTotal; i++){
        final int count = i;
        executorService.execute(() -> {
            try {
                semaphore.acquire();
                add(count);
                semaphore.release();
            catch (InterruptedException e) {
                log.error("exception", e);
            }
            countDownLatch.countDown();
        });
    }
    countDownLatch.await();
    executorService.shutdown();
    log.info("size:{}", list.size());
}


1 回复 有任何疑惑可以回复我~
Jimin 2018-03-24 11:53:50

你好,你运行的结果不是这个例子的。。。。是 HashMapExample 例子的

0 回复 有任何疑惑可以回复我~
问题已解决,确定采纳
还有疑问,暂不采纳
意见反馈 帮助中心 APP下载
官方微信