路过乌云 2018-03-03 14:34 采纳率: 75%
浏览 2373
已采纳

我想知道为什么静态变量锁不住,synchronized(类名.静态变量){}为什么锁不住啊

题目要求是设计4个线程,其中两个线程每次对j增加1,另外两个线程对j每次减少1。我写了两种实现方式,为什么第二种不行
第一种
public class Test {
public static Integer i=0;
public static void main(String[] args) {
Test t=new Test();
ThreadAdd add1=new ThreadAdd(t);
ThreadAdd add2=new ThreadAdd(t);
ThreadReduce reduce1=new ThreadReduce(t);
ThreadReduce reduce2=new ThreadReduce(t);

    add1.start();
    add2.start();
    reduce1.start();
    reduce2.start();
}

}

class ThreadAdd extends Thread{
private Test i;
ThreadAdd(Test j){
i=j;
}

@Override
public void run() {
    while(true){
        synchronized (i){
            i.i=i.i+1;
            System.out.println(Thread.currentThread()+"add="+(i.i));
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

}

}
class ThreadReduce extends Thread{
private Test i;
ThreadReduce(Test j){
i=j;
}

@Override
public void run() {
    while(true){
        synchronized (i){
            i.i=i.i-1;
            System.out.println(Thread.currentThread()+"reduce="+(i.i));
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

}

}
输出:
Thread[Thread-1,5,main]add=1
Thread[Thread-1,5,main]add=2
Thread[Thread-1,5,main]add=3
Thread[Thread-2,5,main]reduce=2
Thread[Thread-2,5,main]reduce=1
Thread[Thread-2,5,main]reduce=0
Thread[Thread-3,5,main]reduce=-1
Thread[Thread-3,5,main]reduce=-2
Thread[Thread-3,5,main]reduce=-3
Thread[Thread-3,5,main]reduce=-4
Thread[Thread-3,5,main]reduce=-5
Thread[Thread-3,5,main]reduce=-6
Thread[Thread-3,5,main]reduce=-7
Thread[Thread-3,5,main]reduce=-8
Thread[Thread-3,5,main]reduce=-9
Thread[Thread-3,5,main]reduce=-10
第二种-------------------------------------------------
public class Test {
public static Integer i=0;
public static void main(String[] args) {
ThreadAdd add1=new ThreadAdd();
ThreadAdd add2=new ThreadAdd();
ThreadReduce reduce1=new ThreadReduce();
ThreadReduce reduce2=new ThreadReduce();

    add1.start();
    add2.start();
    reduce1.start();
    reduce2.start();
}

}

class ThreadAdd extends Thread{
@Override
public void run() {
while(true){
synchronized (Test.i){
Test.i=Test.i+1;
System.out.println(Thread.currentThread()+"add="+(Test.i));
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

}

}
class ThreadReduce extends Thread{

@Override
public void run() {
    while(true){
        synchronized (Test.i){
            Test.i=Test.i-1;
            System.out.println(Thread.currentThread()+"reduce="+(Test.i));
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

}

}
输出结果不符合我的期望,还未知道原因
Thread[Thread-0,5,main]add=1
Thread[Thread-2,5,main]reduce=0
Thread[Thread-2,5,main]reduce=-1
Thread[Thread-2,5,main]reduce=-1
Thread[Thread-1,5,main]add=-1
Thread[Thread-1,5,main]add=-2
Thread[Thread-3,5,main]reduce=-2
Thread[Thread-1,5,main]add=-1
Thread[Thread-2,5,main]reduce=-1
Thread[Thread-0,5,main]add=-1
Thread[Thread-2,5,main]reduce=-2

  • 写回答

5条回答 默认 最新

  • threenewbee 2018-03-03 15:46
    关注

    java规定,不能用synchronized (Test.i)这样静态成员作为锁的,任何教材都会有写,你仔细看看书啊

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

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀