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

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条回答 默认 最新

  • weixin_43110614 2022-03-19 20:02
    关注

    i改成static

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
    1人已打赏
查看更多回答(1条)

报告相同问题?

问题事件

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

悬赏问题

  • ¥15 单纯型python实现编译报错
  • ¥15 c++2013读写oracle
  • ¥15 c++ gmssl sm2验签demo
  • ¥15 关于模的完全剩余系(关键词-数学方法)
  • ¥15 有没有人懂这个博图程序怎么写,还要跟SFB连接,真的不会,求帮助
  • ¥15 PVE8.2.7无法成功使用a5000的vGPU,什么原因
  • ¥15 keil官网下载psn序列号在哪
  • ¥15 想用adb命令做一个通话软件,播放录音
  • ¥30 Pytorch深度学习服务器跑不通问题解决?
  • ¥15 部分客户订单定位有误的问题