各位老师:
以下程序请帮忙看下,观察运行结果始终不能锁定对象,找不出原因,谢谢了。
public class TestSynchronize extends Thread {
Timer timer = new Timer();
public TestSynchronize(String name) {
super(name);
}
public static void main(String[] arg) {
TestSynchronize t1 = new TestSynchronize("t1");
TestSynchronize t2 = new TestSynchronize("t2");
t1.start();
t2.start();
}

public void run() {
  timer.add(Thread.currentThread().getName());
}
}class Timer {
public static int num = 0;
public synchronized void add(String name) {
  //synchronized(this) {
num++;
try {
Thread.sleep(100);
} catch(InterruptedException e) {
e.printStackTrace();
}
System.out.println(name+" num: "+num);
  //}
}
}运行结果:
t1 num: 2
t2 num: 2

解决方案 »

  1.   

    timer对象不是一个,而是两个。
      

  2.   

    搂主可以在System.out.println(name+" num: "+num);后面加一语句:
    System.out.println(this);就看得清楚了,因为timer对象不是一个,所以没法实现互斥。
    楼主可以把timer 对象定成静态的,就能实现同步了。
    static Timer timer = new Timer();
      

  3.   


    package cai.thread;public class TestSynchronize extends Thread {
    public static Timer timer = new Timer(); public TestSynchronize(String name) {
    super(name);
    } public static void main(String[] arg) {
    TestSynchronize t1 = new TestSynchronize("t1");
    TestSynchronize t2 = new TestSynchronize("t2");
    t1.start();
    t2.start();
    } public void run() {
    for (int i = 0; i < 5; i++)
    timer.add(Thread.currentThread().getName());
    }
    }class Timer {
    public static int num = 0; public synchronized void add(String name) {
    // synchronized(this) {
    num++;
    try {
    Thread.sleep(100);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    System.out.println(name + " num: " + num);
    // }
    }
    }把timer定义成类变量 ,两个线程类共享它,才会同步吧,我不知道我理解的对不对
    你创建了两个线程 ,每一个线程里都有一个timer,如果定义为static ,那么两个线程类 共享它,所以才会锁定。
      

  4.   

    锁是对象的内存地址

    TestSynchronize t1 = new TestSynchronize("t1");
    TestSynchronize t2 = new TestSynchronize("t2");
    创建了两个Timer对象
    导致add方法的锁在两个对象上
    Timer timer = new Timer();改为静态的就可以了