java:多线程实测

如下各种组合代码:

public class ExampleUnitTest {
    private AtomicLong aTest1 = new AtomicLong(0);
    private static AtomicLong aTest2 = new AtomicLong(0);
    private volatile AtomicLong aTest3 = new AtomicLong(0);
    private static volatile AtomicLong aTest4 = new AtomicLong(0);
    private static volatile AtomicLong aTest5 = new AtomicLong(0);
    private static volatile AtomicLong aTest6 = new AtomicLong(0);
    private volatile long bTest = 0;
    private long cTest = 0;
    private static long dTest = 0;
    private static volatile long eTest = 0;

    @Test
    public void testMutiThread() throws InterruptedException {
        for (int i = 0; i < 10000; i++) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    for (int b = 0; b < 10000; b++) {
                        aTest1.addAndGet(1);
                        aTest2.addAndGet(1);
                        aTest3.addAndGet(1);
                        aTest4.addAndGet(1);
                        aTest5.incrementAndGet();
                        aTest6.set(aTest6.get() + 1);
                        bTest += 1;
                        cTest += 1;
                        dTest += 1;
                        eTest += 1;
                    }
                }
            }).start();
        }

       sleepPrint();
        sleepPrint();
        sleepPrint();
    }

    private void sleepPrint() throws InterruptedException {
        System.out.println("aTest1=" + aTest1.get());
        System.out.println("aTest2=" + aTest2.get());
        System.out.println("aTest3=" + aTest3.get());
        System.out.println("aTest4=" + aTest4.get());
        System.out.println("aTest5=" + aTest5.get());
        System.out.println("aTest6=" + aTest6.get());
        System.out.println("bTest=" + bTest);
        System.out.println("cTest=" + cTest);
        System.out.println("dTest=" + dTest);
        System.out.println("eTest=" + eTest);
        Thread.sleep(1000);
    }
}

实际日志如下:

aTest1=99937040
aTest2=99982649
aTest3=99983574
aTest4=99984102
aTest5=99984557
aTest6=72110419
bTest=73125502
cTest=79656231
dTest=91396726
eTest=75477497
aTest1=100000000
aTest2=100000000
aTest3=100000000
aTest4=100000000
aTest5=100000000
aTest6=72122513
bTest=73138023
cTest=79668467
dTest=91409613
eTest=75488925
aTest1=100000000
aTest2=100000000
aTest3=100000000
aTest4=100000000
aTest5=100000000
aTest6=72122513
bTest=73138023
cTest=79668467
dTest=91409613
eTest=75488925
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值