import java.util.concurrent.CountDownLatch; import java.util.concurrent.atomic.AtomicInteger; /** * 演示AtomicInteger的使用 */ public class AtomicIntegerTest { private static AtomicInteger count = new AtomicInteger(0); private static void inc(){ try { Thread.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } count.getAndIncrement(); } public static void main(String [] args) throws InterruptedException { final CountDownLatch latch = new CountDownLatch(100); for (int i = 0; i< 100; i++) { new Thread(new Runnable() { @Override public void run() { AtomicIntegerTest.inc(); latch.countDown(); } }).start(); } latch.await(); System.out.println("運行結果:" + count); } }
演示AtomicInteger的使用
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.