CountDownLatch与AtomicInteger以及一些样板代码

huangapple 未分类评论50阅读模式
标题翻译

CountDownLatch vs AtomicInteger and some boilerplate

问题

以下是您提供的代码的翻译部分:

可以在这个示例中使用带有同步和volatile特性的AtomicInteger来代替CountDownLatch吗我有遗漏了什么吗是否还有其他情况在其中我不能用AtomicInteger替换CountDownLatch

static void runProducer(final int sendMessageCount) throws InterruptedException {
    final Producer<Long, String> producer = createProducer();
    long time = System.currentTimeMillis();
    final CountDownLatch countDownLatch = new CountDownLatch(sendMessageCount);

    try {
        for (long index = time; index < time + sendMessageCount; index++) {
            final ProducerRecord<Long, String> record =
                    new ProducerRecord<>(TOPIC, index, "Hello Mom " + index);
            producer.send(record, (metadata, exception) -> {
                long elapsedTime = System.currentTimeMillis() - time;
                if (metadata != null) {
                    System.out.printf("已发送记录(key=%s value=%s) " +
                                    "元数据(分区=%d, 偏移量=%d) 时间=%d\n",
                            record.key(), record.value(), metadata.partition(),
                            metadata.offset(), elapsedTime);
                } else {
                    exception.printStackTrace();
                }
                countDownLatch.countDown();
            });
        }
        countDownLatch.await(25, TimeUnit.SECONDS);
    } finally {
        producer.flush();
        producer.close();
    }
}
英文翻译

Can I use AtomicInteger (Which is Synchronized and volatile) instead of countdownLatch in this example. Am I missing something? Is there any other scenario where I cannot replace the countdownLatch with an AtomicInteger?

static void runProducer(final int sendMessageCount) throws InterruptedException {
    final Producer&lt;Long, String&gt; producer = createProducer();
    long time = System.currentTimeMillis();
    final CountDownLatch countDownLatch = new CountDownLatch(sendMessageCount);

    try {
        for (long index = time; index &lt; time + sendMessageCount; index++) {
            final ProducerRecord&lt;Long, String&gt; record =
                    new ProducerRecord&lt;&gt;(TOPIC, index, &quot;Hello Mom &quot; + index);
            producer.send(record, (metadata, exception) -&gt; {
                long elapsedTime = System.currentTimeMillis() - time;
                if (metadata != null) {
                    System.out.printf(&quot;sent record(key=%s value=%s) &quot; +
                                    &quot;meta(partition=%d, offset=%d) time=%d\n&quot;,
                            record.key(), record.value(), metadata.partition(),
                            metadata.offset(), elapsedTime);
                } else {
                    exception.printStackTrace();
                }
                countDownLatch.countDown();
            });
        }
        countDownLatch.await(25, TimeUnit.SECONDS);
    }finally {
        producer.flush();
        producer.close();
    }
}

huangapple
  • 本文由 发表于 2020年3月17日 03:29:10
  • 转载请务必保留本文链接:https://java.coder-hub.com/60712132.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定