zoukankan      html  css  js  c++  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();
                
            }
    
        }
    
    }
  • 相关阅读:
    批量删除.svn文件夹、.svn文件
    Windows 7下Git SSH 创建Key的步骤
    Git:本地项目与远程仓库的git/clone
    git解决二进制文件冲突
    git设置mergetool可视化工具
    redhat7.2配置yum源
    project2016安装与破解
    strace 使用案例
    运维老鸟教你安装centos6.5如何选择安装包
    CSS限制
  • 原文地址:https://www.cnblogs.com/huhuuu/p/6204044.html
Copyright © 2011-2022 走看看