提交 4bf4681d 编写于 作者: C chengxiangwang

use putIfAbsent in MomentStatsItemSet.java

上级 b2544b5f
...@@ -41,7 +41,14 @@ public class BrokerStatsManagerTest { ...@@ -41,7 +41,14 @@ public class BrokerStatsManagerTest {
} }
} }
public AtomicLong test_unit() throws InterruptedException { @Test
public void test_getAndCreateMomentStatsItem_multiThread() throws InterruptedException {
for (int i = 0; i < 5; i++) {
assertEquals(10, test_unit_moment().longValue());
}
}
private AtomicLong test_unit() throws InterruptedException {
brokerStatsManager = new BrokerStatsManager("DefaultCluster"); brokerStatsManager = new BrokerStatsManager("DefaultCluster");
executor = new ThreadPoolExecutor(100, 200, 10, TimeUnit.SECONDS, executor = new ThreadPoolExecutor(100, 200, 10, TimeUnit.SECONDS,
new ArrayBlockingQueue<Runnable>(10000), new ThreadFactoryImpl("testMultiThread")); new ArrayBlockingQueue<Runnable>(10000), new ThreadFactoryImpl("testMultiThread"));
...@@ -62,6 +69,27 @@ public class BrokerStatsManagerTest { ...@@ -62,6 +69,27 @@ public class BrokerStatsManagerTest {
return brokerStatsManager.getStatsItem(TOPIC_PUT_NUMS, "topicTest").getValue(); return brokerStatsManager.getStatsItem(TOPIC_PUT_NUMS, "topicTest").getValue();
} }
private AtomicLong test_unit_moment() throws InterruptedException {
brokerStatsManager = new BrokerStatsManager("DefaultCluster");
executor = new ThreadPoolExecutor(100, 200, 10, TimeUnit.SECONDS,
new ArrayBlockingQueue<Runnable>(10000), new ThreadFactoryImpl("testMultiThread"));
for (int i = 0; i < 10000; i++) {
executor.submit(new Runnable() {
@Override
public void run() {
brokerStatsManager.getMomentStatsItemSetFallTime().setValue("test",10);
}
});
}
while (true) {
if (executor.getCompletedTaskCount() == 10000) {
break;
}
Thread.sleep(1000);
}
return brokerStatsManager.getMomentStatsItemSetFallTime().getAndCreateStatsItem("test").getValue();
}
@After @After
public void shutdown() { public void shutdown() {
executor.shutdown(); executor.shutdown();
......
...@@ -79,10 +79,10 @@ public class MomentStatsItemSet { ...@@ -79,10 +79,10 @@ public class MomentStatsItemSet {
if (null == statsItem) { if (null == statsItem) {
statsItem = statsItem =
new MomentStatsItem(this.statsName, statsKey, this.scheduledExecutorService, this.log); new MomentStatsItem(this.statsName, statsKey, this.scheduledExecutorService, this.log);
MomentStatsItem prev = this.statsItemTable.put(statsKey, statsItem); MomentStatsItem prev = this.statsItemTable.putIfAbsent(statsKey, statsItem);
if (null == prev) {
if (null != prev) {
statsItem = prev;
// statsItem.init(); // statsItem.init();
} }
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册