class Demo implements Runnable
{
Object obj = new Object();
public void run() {
......
synchronized(obj){
......
}
}
}
}
class ThreadDemo{
public static void main(String[] args){
Demo d = new Demo();
Thread t1 = new Thread(d);
Thread t2 = new Thread(d);
Thread t3 = new Thread(d);
t1.start();
t2.start();
t3.start();
}
}
用实现Runnable接口的方法,在同步代码块内,创建一个Object类的锁,不是每个线程分别创建了自己的Object对象么,为什么能共用同一个锁? |
|