• synchronized和vilatile


    第一个程序

    public class Test06 implements Runnable{
    
        public int a = 0;
        
        public static void main(String[] args) throws InterruptedException {
            Test06 r = new Test06();
            Thread[] t = new Thread[100];
            for(int i = 0;i < 100;i++)
                t[i] = new Thread(r);
            for(int j = 0;j < 100;j++)
                t[j].start();
            Thread.currentThread().sleep(2000);
            System.out.println(r.a);
    
        }
    
        @Override
        public  void run() {
            int b = a+1;
            try {
                Thread.currentThread().sleep(10);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            a = b;
        }
    
    }

    输出为?

    第二个程序

    public class Test06 implements Runnable{
    
        public int a = 0;
        
        public static void main(String[] args) throws InterruptedException {
            Test06 r = new Test06();
            Thread[] t = new Thread[100];
            for(int i = 0;i < 100;i++)
                t[i] = new Thread(r);
            for(int j = 0;j < 100;j++)
                t[j].start();
            Thread.currentThread().sleep(2000);
            System.out.println(r.a);
    
        }
    
        @Override
        public  synchronized void run() {
            int b = a+1;
            try {
                Thread.currentThread().sleep(10);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            a = b;
        }
    
    }

    输出为?

    第三个程序

    public class Test06 implements Runnable{
    
        public volatile int a = 0;
        
        public static void main(String[] args) throws InterruptedException {
            Test06 r = new Test06();
            Thread[] t = new Thread[100];
            for(int i = 0;i < 100;i++)
                t[i] = new Thread(r);
            for(int j = 0;j < 100;j++)
                t[j].start();
            Thread.currentThread().sleep(2000);
            System.out.println(r.a);
    
        }
    
        @Override
        public  void run() {
            int b = a+1;
            try {
                Thread.currentThread().sleep(10);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            a = b;
        }
    
    }

    输出为?

    三个程序中只有第二个能保证输出的为100

  • 相关阅读:
    SQLPrompt 7.2发布
    安装SQL Server2016正式版
    Coping with the TCP TIME-WAIT state on busy Linux servers
    分区里的inode号是0号和1号的block
    如何知道SQL Server机器上有多少个NUMA节点
    使用开源软件的原因
    一起来测试天兔Lepus3.8 Beta版本的MSSQL部分
    新书到手 TRANSACTION PROCESSING:CONCEPTS AND TECHNIQUES
    在线即时展现 Html、JS、CSS 编辑工具
    在触屏设备上面利用html5裁剪图片
  • 原文地址:https://www.cnblogs.com/YESheng/p/3659418.html
Copyright © 2020-2023  润新知