提交 6fc3339f 编写于 作者: 武汉红喜's avatar 武汉红喜

concurrent test

上级 c4a8a3aa
......@@ -4,8 +4,10 @@ import java.util.concurrent.atomic.AtomicLong;
/**
* @author shenhongxi 2019/09/01
* @see LongAdderTest
* @see LongAccumulatorTest
*/
public class AtomicTest {
public class AtomicLongTest {
private static AtomicLong count = new AtomicLong();
......
package org.hongxi.java.util.concurrent;
import java.util.concurrent.atomic.LongAccumulator;
/**
* @author shenhongxi 2019/09/01
*/
public class LongAccumulatorTest {
private static LongAccumulator count = new LongAccumulator((left, right) -> left + right, 0);
private static Integer[] arrayOne = new Integer[] {0, 1, 2, 3, 0, 5, 6, 0, 56, 0};
private static Integer[] arrayTwo = new Integer[] {10, 1, 2, 3, 0, 5, 6, 0, 56, 0};
public static void main(String[] args) throws InterruptedException {
Thread threadOne = new Thread(() -> {
int size = arrayOne.length;
for (int i = 0; i < size; i++) {
if (arrayOne[i].intValue() == 0)
count.accumulate(1);
}
});
Thread threadTwo = new Thread(() -> {
int size = arrayTwo.length;
for (int i = 0; i < size; i++) {
if (arrayTwo[i].intValue() == 0)
count.accumulate(1);
}
});
threadOne.start();
threadTwo.start();
// 等待线程执行完毕
threadOne.join();
threadTwo.join();
System.out.println("count 0:" + count.get());
}
}
package org.hongxi.java.util.concurrent;
import java.util.concurrent.atomic.LongAdder;
/**
* @author shenhongxi 2019/09/01
*/
public class LongAdderTest {
private static LongAdder count = new LongAdder();
private static Integer[] arrayOne = new Integer[] {0, 1, 2, 3, 0, 5, 6, 0, 56, 0};
private static Integer[] arrayTwo = new Integer[] {10, 1, 2, 3, 0, 5, 6, 0, 56, 0};
public static void main(String[] args) throws InterruptedException {
Thread threadOne = new Thread(() -> {
int size = arrayOne.length;
for (int i = 0; i < size; i++) {
if (arrayOne[i].intValue() == 0)
count.increment();
}
});
Thread threadTwo = new Thread(() -> {
int size = arrayTwo.length;
for (int i = 0; i < size; i++) {
if (arrayTwo[i].intValue() == 0)
count.increment();
}
});
threadOne.start();
threadTwo.start();
// 等待线程执行完毕
threadOne.join();
threadTwo.join();
System.out.println("count 0:" + count.sum());
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册