/**
* @Description
* @Author
* @Date 2015-5-8 上午9:37:45
*/
public class ConcurrentAtomicTest {
private final static AtomicInteger atomicInteger=new AtomicInteger(1);
private static volatile int integer=1;;
/**
* @Description TODO
* @Return void
* @Throws
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//testAtomicInteger();
testInteger2();
}
public static void testAtomicInteger() {
String time = new SimpleDateFormat("HH:mm:ss").format(new Date());
System.out.println("Start time : " + time);
int temp=atomicInteger.addAndGet(1);//add
atomicInteger.get();//get current
System.out.print(temp+",");
temp=atomicInteger.decrementAndGet();//Atomically decrements by one the current value.
System.out.print(temp+",");
temp=atomicInteger.getAndIncrement();//Atomically decrements by one the current value.
System.out.print(temp+",");
temp=atomicInteger.intValue();//Atomically decrements by one the current value.
System.out.print(temp+",");
atomicInteger.set(0);//Atomically decrements by one the current value.
temp=atomicInteger.get();
System.out.println(temp+",");
//result:2,1,1,2,0,
ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(5); // 创建5个执行线程
Runnable runnable = new Runnable() {
@Override
public void run() {
String time = new SimpleDateFormat("HH:mm:ss").format(new Date());
atomicInteger.incrementAndGet();
System.out.println("Now Time : " + time+",integer:"+integer);
}
};