首页 > 代码库 > 并发时候的测试程序
并发时候的测试程序
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(); } }}
并发时候的测试程序
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。