码迷,mamicode.com
首页 > 其他好文 > 详细

并发时候的测试程序

时间:2016-12-20 20:42:07      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:ref   rate   rup   auto   测试程序   nbsp   runnable   oid   java   

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();
            
        }

    }

}

 

并发时候的测试程序

标签:ref   rate   rup   auto   测试程序   nbsp   runnable   oid   java   

原文地址:http://www.cnblogs.com/huhuuu/p/6204044.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!