java.util.concurrent.atomic包测试
java.util.concurrent.atomic 包下的class都是原子操作的,这里简单易多线程测试一下.分别测试用普通和用原子的不同
首先先看一下不是原子操作代码和结果
package com.java.base;
public class IntegerByCommon {
//创建三个线程同时执行一个资源
public static void main(String[] args) {
TestThread testThread = new TestThread(100);
Thread one = new Thread(testThread);
Thread two = new Thread(testThread);
Thread three = new Thread(testThread);
one.start();
two.start();
three.start();
}
}
//创建任务
class TestThread implements Runnable{
private int inte;
public TestThread(int inte) {
this.inte = inte;
}
public void run() {
//循环
while (inte>0) {
try {
//模拟业务代码
Thread.sleep(10l);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//操作共享资源
inte--;
System.out.println(Thread.currentThread().getName()+"----"+inte);
}
}
}
结果只取一部分说明问题:
Thread-1----10
Thread-0----7
Thread-1----7
Thread-2----7
Thread-0----6
Thread-2----5
Thread-1----5
Thread-0----3
Thread-1----3
Thread-2----3
Thread-0----2
Thread-2----1
Thread-1----1
Thread-2-----1
Thread-1-----1
Thread-0-----1
发现有重复消费,还很严重.
下面看一下原子操作
1. 类 AtomicInteger
package com.java.base;
import java.util.concurrent.atomic.AtomicInteger;
public class IntegerByAtomic {
//创建三个线程同时执行一个资源
public static void main(String[] args) {
//定义一个全局变量
AtomicInteger integer = new AtomicInteger(100);
TestThread1 t = new TestThread1(integer);
Thread one = new Thread(t);
Thread two = new Thread(t);
Thread three = new Thread(t);
one.start();
two.start();
three.start();
}
}
//创建任
class TestThread1 implements Runnable{
private AtomicInteger inte;
public TestThread1(AtomicInteger integer) {
this.inte = integer;
}
public void run() {
try {
//循环
while (inte.get()>0) {
//模拟业务
Thread.sleep(10l);
//防止过度消费
if (inte.get()<1) {
inte=null;
return;
}
Integer aa = inte.decrementAndGet();
System.out.println(Thread.currentThread().getName()+"****"+aa);
}
} catch (Exception e) {
System.out.println("完了");
return;
}
}
}
结果为:
Thread-1****12
Thread-0****10
Thread-2****9
Thread-1****8
Thread-0****7
Thread-2****6
Thread-1****5
Thread-0****4
Thread-2****2
Thread-1****3
Thread-0****1
Thread-2****0
2.类 AtomicIntegerArray
package com.java.base;
import java.util.concurrent.atomic.AtomicIntegerArray;
public class IntegerArrayAtomic {
public static void main(String[] args) {
// 定义一个全局变量
int[] arr= {100,10};
AtomicIntegerArray integer = new AtomicIntegerArray(arr);
TestThread2 t = new TestThread2(integer);
Thread one = new Thread(t);
Thread two = new Thread(t);
Thread three = new Thread(t);
one.start();
two.start();
three.start();
}
}
// 创建任
class TestThread2 implements Runnable {
private AtomicIntegerArray inte;
public TestThread2(AtomicIntegerArray integer) {
this.inte = integer;
}
public void run() {
try {
// 循环
while (inte.get(0) > 0) {
// 模拟业务
Thread.sleep(10l);
// 防止过度消费
if (inte.get(0) < 1) {
inte = null;
return;
}
Integer aa = inte.decrementAndGet(0);
System.out.println(Thread.currentThread().getName() + "****" + aa);
}
} catch (Exception e) {
System.out.println("完了");
return;
}
}
}
结果是:
Thread-0****10
Thread-1****12
Thread-2****11
Thread-2****9
Thread-1****8
Thread-0****7
Thread-0****6
Thread-1****4
Thread-2****5
Thread-1****1
Thread-2****3
Thread-0****2
Thread-1****0
发现原子操作,可以保证线程安全.
本文如有疏漏和错误之处,请各位不吝赐教,特此感谢。