java - 线程同步为什么不一样

 星晴SOS 发布于 2022-10-25 09:01

package com.dome;

public class Thread01 {

private volatile  static int a =10;
Thread td1 = new Thread(){
    
    public void run(){
        
        for(int i=0;i<3;i++){
            
                a = a+1;
            
            System.out.println(i+"td1:="+a);
        }
        
    }
    
};

Thread td2 = new Thread(){
    public void run(){
        
        for(int i=0;i<3;i++){
            a -=1;
            System.out.println(i+"td2:="+a);
        }
    }
    
};


public static void main(String[] args) {
    Thread01 th = new Thread01();
    th.td1.start();
    
    th.td2.start();
    
}

}

0td1:=9
0td2:=9
1td1:=10
1td2:=9
2td1:=10
2td2:=9

1 个回答
  • a = a + 1a = a - 1 这样的语句,事实上涉及了 读取-修改-写入 三个操作:

    1. 读取变量到栈中某个位置

    2. 对栈中该位置的值进行加 (减)1

    3. 将自增后的值写回到变量对应的存储位置

    因此虽然变量 a 使用 volatile 修饰,但并不能使涉及上面三个操作的 a = a + 1a = a - 1具有原子性。为了保证同步性,需要使用 synchronized

    public class Thread01 {
    
        private volatile static int a = 10;
    
        Thread td1 = new Thread() {
    
            public void run() {
    
                for (int i = 0; i < 3; i++) {
                    synchronized (Thread01.class) {
                        a = a + 1;
                        System.out.println(i + "td1:=" + a);
                    }
                }
            }
    
        };
    
        Thread td2 = new Thread() {
            public void run() {
    
                for (int i = 0; i < 3; i++) {
                    synchronized (Thread01.class) {
                        a -= 1;
                        System.out.println(i + "td2:=" + a);
                    }
                }
            }
    
        };
    
        public static void main(String[] args) {
            Thread01 th = new Thread01();
            th.td1.start();
    
            th.td2.start();
    
        }
    }

    某次运行结果:

    (td1 出现的地方,a 就 +1;td2 出现的地方,a 就 -1)

    2022-10-26 14:42 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有