Java Synchronized(object) 无效

直接贴代码:


public class TestSync implements Runnable {

    Timer timer = new Timer();

    @Override
    public void run() {

        if ("t1".equals(Thread.currentThread().getName())) {
            timer.add(Thread.currentThread().getName());
        } else {
            timer.add2(Thread.currentThread().getName());
        }
    }

    public static void main(String[] args) {
        TestSync test = new TestSync();
        Thread t1 = new Thread(test);
        Thread t2 = new Thread(test);
        t1.setName("t1");
        t2.setName("t2");
        t1.start();
        t2.start();
    }

}

class Timer {
    private /*static*/ Integer num = 0;

    public void add(String name) {
        synchronized (num) {
            num++;
            try {
                Thread.sleep(20);
            } catch (InterruptedException e) {
            }
            System.out.println(name + ", num = " + num);
        }
    }

    public void add2(String name) {
        synchronized (num) {
            num++;
            try {
                Thread.sleep(2);
            } catch (InterruptedException e) {
            }
            System.out.println(name + ", num2 = " + num);
        }
    }
}

为什么有时候出来的结果是:
t2, num2 = 2
t1, num = 2

不应该是下面这个吗?
t1, num = 1
t2, num2 = 2

我知道了:因为Integer执行自加操作的时候,是要进行拆箱操作即调用Integer的intValue返回基本类型数据,执行完自增以后,再调用Integer的装箱操作即调用valueOf重新创建对象。其实已经不是一个对象了。