static 和 volatile区别

区别

https://www.zhihu.com/question/41579791

 

一些有关于volatile的文章

http://www.cnblogs.com/xrq730/p/7048693.html

http://blog.csdn.net/yechaodechuntian/article/details/26009363

http://www.cnblogs.com/zemliu/p/3298685.html

 

单例模式中的volatile

http://jiangzhengjun.iteye.com/blog/652440

http://blog.csdn.net/anjxue/article/details/51038466

 

java6下执行   ( java8下会无论加不加volatile都会退出循环)

public static void main(String[] args) throws InterruptedException {

    RunningThread runningThread = new RunningThread();
    runningThread.start();
    VolatileRunningThread volatileRunningThread = new VolatileRunningThread();
    volatileRunningThread.start();


    Thread.sleep(1000);

    runningThread.isRunning = false;
    volatileRunningThread.isRunning = false;


}

static class RunningThread extends Thread {

    public Boolean isRunning;

    int i = 0; // 改成Integer会退出循环  不知道为什么? 

    @Override
    public void run() {
        isRunning = true;

        while (isRunning) {
            i++;
        }

        System.out.println(i);

        System.out.println("RunningThread退出循环 isRunning=" + isRunning);
    }
}

static class VolatileRunningThread extends Thread {

    public volatile Boolean isRunning;

    @Override
    public void run() {
        isRunning = true;

        while (isRunning) {
            ;
        }

        System.out.println("VolatileRunningThread退出循环 isRunning=" + isRunning);
    }
}

 

public class TestVolatile  extends Thread{

    static int i = 0;

    private static boolean stopRequested;


    @Override
    public void run() {
        for (int j = 0; j < 100000; j++) {
            i++;
        }
    }

    public static void main(String[] args) throws InterruptedException {
        dsa();
    }

    private static void aaa() throws InterruptedException {
        Thread backgroundThread = new Thread(new Runnable()
        {
            public void run()
            {
                int i = 0;
                while (!stopRequested) {
                    i++;
                }
            }
        });

        backgroundThread.start();

        TimeUnit.SECONDS.sleep(1);
        stopRequested = true;
    }

    /**
     * 使用volatile修饰i  输出2809657
     * 使用static修饰i或者不修饰i 输出4701547
     *
     */
    private static void dsa() {
        TestVolatile testVolatile = new TestVolatile();

        List<Thread> list = new ArrayList<Thread>();
        for (int i = 0; i < 100; i++) {
            Thread thread = new Thread(testVolatile);
            list.add(thread);
        }
        for (Thread thread : list) thread.start();

        f:for (;;) {
            for (Thread thread : list )
                if (thread.isAlive()) continue f;
            break;
        }

        System.out.println(testVolatile.i);
    }


}

转载于:https://my.oschina.net/zhuqianli/blog/1083110

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值