内容简介:本文主要研究一下Elasticsearch的RunOnceelasticsearch-7.0.1/server/src/main/java/org/elasticsearch/common/util/concurrent/RunOnce.javaelasticsearch-7.0.1/server/src/test/java/org/elasticsearch/common/util/concurrent/RunOnceTests.java
序
本文主要研究一下Elasticsearch的RunOnce
RunOnce
elasticsearch-7.0.1/server/src/main/java/org/elasticsearch/common/util/concurrent/RunOnce.java
public class RunOnce implements Runnable { private final Runnable delegate; private final AtomicBoolean hasRun; public RunOnce(final Runnable delegate) { this.delegate = Objects.requireNonNull(delegate); this.hasRun = new AtomicBoolean(false); } @Override public void run() { if (hasRun.compareAndSet(false, true)) { delegate.run(); } } /** * {@code true} if the {@link RunOnce} has been executed once. */ public boolean hasRun() { return hasRun.get(); } }
- RunOnce实现了Runnable接口,它的构造器要求输入Runnable,同时构造了hasRun变量;run方法会先使用compareAndSet将hasRun由false设置为true,如果成功则执行代理的Runnable的run方法;hasRun方法则返回hasRun值
实例
elasticsearch-7.0.1/server/src/test/java/org/elasticsearch/common/util/concurrent/RunOnceTests.java
public class RunOnceTests extends ESTestCase { public void testRunOnce() { final AtomicInteger counter = new AtomicInteger(0); final RunOnce runOnce = new RunOnce(counter::incrementAndGet); assertFalse(runOnce.hasRun()); runOnce.run(); assertTrue(runOnce.hasRun()); assertEquals(1, counter.get()); runOnce.run(); assertTrue(runOnce.hasRun()); assertEquals(1, counter.get()); } public void testRunOnceConcurrently() throws InterruptedException { final AtomicInteger counter = new AtomicInteger(0); final RunOnce runOnce = new RunOnce(counter::incrementAndGet); final Thread[] threads = new Thread[between(3, 10)]; final CountDownLatch latch = new CountDownLatch(1); for (int i = 0; i < threads.length; i++) { threads[i] = new Thread(() -> { try { latch.await(); } catch (InterruptedException e) { throw new RuntimeException(e); } runOnce.run(); }); threads[i].start(); } latch.countDown(); for (Thread thread : threads) { thread.join(); } assertTrue(runOnce.hasRun()); assertEquals(1, counter.get()); } public void testRunOnceWithAbstractRunnable() { final AtomicInteger onRun = new AtomicInteger(0); final AtomicInteger onFailure = new AtomicInteger(0); final AtomicInteger onAfter = new AtomicInteger(0); final RunOnce runOnce = new RunOnce(new AbstractRunnable() { @Override protected void doRun() throws Exception { onRun.incrementAndGet(); throw new RuntimeException("failure"); } @Override public void onFailure(Exception e) { onFailure.incrementAndGet(); } @Override public void onAfter() { onAfter.incrementAndGet(); } }); final int iterations = randomIntBetween(1, 10); for (int i = 0; i < iterations; i++) { runOnce.run(); assertEquals(1, onRun.get()); assertEquals(1, onFailure.get()); assertEquals(1, onAfter.get()); assertTrue(runOnce.hasRun()); } } }
- testRunOnce方法验证了顺序多次执行runOnce的场景;testRunOnceConcurrently方法则验证了并发多次执行runOnce的场景;testRunOnceWithAbstractRunnable则验证了使用AbstractRunnable作为runnable的场景
小结
RunOnce实现了Runnable接口,它的构造器要求输入Runnable,同时构造了hasRun变量;run方法会先使用compareAndSet将hasRun由false设置为true,如果成功则执行代理的Runnable的run方法;hasRun方法则返回hasRun值
doc
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
锋利的jQuery
单东林、张晓菲、魏然 / 人民邮电出版社 / 2009-6 / 39.00元
《锋利的jQuery》循序渐进地对jQuery的各种函数和方法调用进行了介绍,读者可以系统地掌握jQuery的DOM操作、事件监听和动画、表单操作、AJAX以及插件方面等知识点,并结合每个章节后面的案例演示进行练习,达到掌握核心知识点的目的。为使读者更好地进行开发实践,《锋利的jQuery》的最后一章将前7章讲解的知识点和效果进行了整合,打造出一个非常有个性的网站,并从案例研究、网站材料、网站结构......一起来看看 《锋利的jQuery》 这本书的介绍吧!