代码如下:
package myss1;
public class TestSynchronized {
public static void main(String[] args) {
D d1=new D();
D d2=new D();
d1.setName("d1");
d2.setName("d2");
d1.start();
d2.start();
}
}
class D extends Thread{
private static int num=0;
public void run(){
synchronized (this){
num++;
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(getName()+" "+num);
}
}
}
我预想的输出结果是:
d1 1
d2 2
可是输出结构是:
d1 2
d2 2
为什么啊,我明明锁住了那段代码了啊!