首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

添不加Volatile看不出有啥效果啊

2012-10-25 
加不加Volatile看不出有啥效果啊?import java.util.concurrent.atomic.AtomicIntegerpublic class Volati

加不加Volatile看不出有啥效果啊?

import java.util.concurrent.atomic.AtomicInteger;public class VolatilePattern1 extends Thread{volatile boolean shutdownRequested;private AtomicInteger count = new AtomicInteger();public void shutdown() {shutdownRequested = true;}public void run() { System.out.println("Thread:" + Thread.currentThread().getName()+" started.");    while (!shutdownRequested) {         System.out.println("working ..."+count);        count.getAndIncrement();        try {Thread.sleep(50);} catch(InterruptedException ie) {ie.printStackTrace();}    }}public static void main(String[] args) {System.out.println("Thread:" + Thread.currentThread().getName()+" started.");VolatilePattern1 vp = new VolatilePattern1();vp.start();try {Thread.sleep(2000);} catch(InterruptedException ie) {ie.printStackTrace();} finally {vp.shutdown();}}}

热点排行