xcdsdf14545
路过乌云
2018-03-03 14:34

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

5
  • 线程
  • android
  • thread

题目要求是设计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条回答