zgcy123456 2022-03-19 17:32 采纳率: 50%
浏览 98
已结题

java synchronized 类锁不起作用

请大家帮忙看看这段代码中 synchronized 为什么没有起作用。

锁添加的是类级别的,为什么锁没有起作用,两个线程都可以访问。

public class AccountingSync implements Runnable{

    int i =0;
    //Integer k = 0;
    static Integer sk = 0;

    public void increase(){
        System.out.println(Thread.currentThread().getName() + " before  " + i);
        i++;
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() + " after  " + i);
    }

    @Override
    public void run() {
        for (int j =0; j< 10; j++){
            synchronized (sk){
                increase();
            }
        }
    }

    public static void main(String[] args) {
        AccountingSync instance = new AccountingSync();
        AccountingSync instance1 = new AccountingSync();
        Thread t1 = new Thread(instance);
        Thread t2 = new Thread(instance1);

        t1.start();
        t2.start();

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

运行结果及报错内容

Thread-0 before 0
Thread-0 after 1
Thread-1 before 0
Thread-1 after 1
Thread-1 before 1
Thread-1 after 2
Thread-1 before 2
Thread-1 after 3
Thread-1 before 3
Thread-1 after 4
Thread-1 before 4
Thread-1 after 5
Thread-1 before 5
Thread-1 after 6
Thread-1 before 6
Thread-1 after 7
Thread-1 before 7
Thread-1 after 8
Thread-1 before 8
Thread-1 after 9
Thread-0 before 1
Thread-0 after 2
Thread-1 before 9
Thread-1 after 10
Thread-0 before 2
Thread-0 after 3
Thread-0 before 3
Thread-0 after 4
Thread-0 before 4
Thread-0 after 5
Thread-0 before 5
Thread-0 after 6
Thread-0 before 6
Thread-0 after 7
Thread-0 before 7
Thread-0 after 8
Thread-0 before 8
Thread-0 after 9
Thread-0 before 9
Thread-0 after 10

我的解答思路和尝试过的方法
我想要达到的结果

Thread-0 before 0
Thread-0 after 1
Thread-0 before 1
Thread-0 after 2
Thread-0 before 2
Thread-0 after 3
Thread-0 before 3
Thread-0 after 4
Thread-1 before 4
Thread-1 after 5
Thread-1 before 5
Thread-1 after 6
Thread-1 before 6
Thread-1 after 7
Thread-1 before 7
Thread-1 after 8
Thread-1 before 8
Thread-1 after 9
Thread-0 before 9
Thread-0 after 10
Thread-1 before 10
Thread-1 after 11
Thread-1 before 11
Thread-1 after 12
Thread-1 before 12
Thread-1 after 13
Thread-1 before 13
Thread-1 after 14
Thread-1 before 14
Thread-1 after 15
Thread-0 before 15
Thread-0 after 16
Thread-0 before 16
Thread-0 after 17
Thread-0 before 17
Thread-0 after 18
Thread-0 before 18
Thread-0 after 19
Thread-0 before 19
Thread-0 after 20

  • 写回答

2条回答

      报告相同问题?

      相关推荐 更多相似问题

      问题事件

      • 系统已结题 3月27日
      • 已采纳回答 3月19日
      • 修改了问题 3月19日
      • 创建了问题 3月19日

      悬赏问题

      • ¥15 使用DWY100k数据集对UEA进行测试,出现报错:IndexError: index 125000 is out of bounds for axis 0 with size 95500
      • ¥15 前端vue实现根据图片url生成pdf文件
      • ¥15 RfidReader资源Q个
      • ¥20 user-agent是否是唯一的,有没有可能相同
      • ¥15 关于#开会#的问题,如何解决?(语言-c++)
      • ¥15 关于#二十四点问题#的问题
      • ¥15 运行kitex的demon出错(求大家解决)
      • ¥15 开发一个类似 typora 这样的软件需要多少钱?
      • ¥15 clearcase7.0安装包
      • ¥15 断点回归模型月度核密度检验不连续