并发时候的测试程序

Posted 雕刻时光

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了并发时候的测试程序相关的知识,希望对你有一定的参考价值。

package concurrency;

import java.util.concurrent.atomic.AtomicReference;

public class TestConcurrency {

    private static volatile Integer         num1 = 0;
    private static Integer         num2 = 0;
    private static AtomicReference<Integer> ar   = new AtomicReference<Integer>(num1);

    public static void dfasd111() throws InterruptedException {
        for (int i = 0; i < 1000; i++) {
            new Thread(new Runnable() {

                public void run() {
                    for (int i = 0; i < 10000; i++)
                        while (true) {
                            Integer temp = ar.get();
                            if (ar.compareAndSet(temp, temp + 1)) break;
                        }
                }
            }).start();
        }
        Thread.sleep(10000);
        System.out.println(ar.get()); // 10000000
    }

    public static void dfasd1112() throws InterruptedException {
        for (int i = 0; i < 1000; i++) {
            new Thread(new Runnable() {

                public void run() {
                    for (int i = 0; i < 10000; i++) {
                        num1 = num1++;
                    }
                }
            }).start();
        }
        Thread.sleep(10000);
        System.out.println(num1); // something like 238981
    }

    public static void funnum2() throws InterruptedException {
        for (int i = 0; i < 1000; i++) {
            new Thread(new Runnable() {

                public void run() {
                    for (int i = 0; i < 10000; i++) {
                        num2 = num2++;
                    }
                }
            }).start();
        }
        Thread.sleep(10000);
        System.out.println(num2); // something like 238981
    }
    
    public static void main(String[] args) {

        try {
           // dfasd111();
            //dfasd1112();
            funnum2();
        } catch (InterruptedException e) {
            
            // TODO Auto-generated catch block e.printStackTrace();
            
        }

    }

}

 

以上是关于并发时候的测试程序的主要内容,如果未能解决你的问题,请参考以下文章

Swift新async/await并发中利用Task防止指定代码片段执行的数据竞争(Data Race)问题

并发时候的测试程序

全栈编程系列SpringBoot整合Shiro(含KickoutSessionControlFilter并发在线人数控制以及不生效问题配置启动异常No SecurityManager...)(代码片段

golang goroutine例子[golang并发代码片段]

Apache ab 压力并发测试工具

Apache ab 压力并发测试工具