呃。。記錄一個比較奇怪的現象。。。待於以後實際開發中測試後選擇
package cn.itcast.heima2;
import java.util.concurrent.atomic.AtomicInteger;
public class AtomicIntegerCompareTest {
private int value;
public AtomicIntegerCompareTest(int value){
this.value = value;
}
public synchronized int increase(){
return value++;
}
public static void main(String args[]){
long start = System.currentTimeMillis();
AtomicIntegerCompareTest test = new AtomicIntegerCompareTest(0);
for( int i=0;i< 1000000;i++){
test.increase();
}
long end = System.currentTimeMillis();
System.out.println("time elapse:"+(end -start));
long start1 = System.currentTimeMillis();
AtomicInteger atomic = new AtomicInteger(0);
for( int i=0;i< 1000000;i++){
atomic.incrementAndGet();
}
long end1 = System.currentTimeMillis();
System.out.println("time elapse:"+(end1 -start1) );
/*
* JRE 1.6.0_12(32位):
* time elapse:34
* time elapse:14
*
* jdk1.7.0_51(64位):
* time elapse:17
* time elapse:19
*/
}
}
在7中concurrent包中的atomic竟然沒有了高效執行的優勢?????放在以後測試後決定使用哪個吧。。。