public class Handel1 extends Thread{
public void run(){
try{
Counter counter = Counter.getInstance();
counter.lock("Handel1");
}catch(Exception e){
e.printStackTrace();
}
}
}public class Handel2 extends Thread {
public void run(){
try{
Counter counter = Counter.getInstance();
counter.lock("Handel2");
}catch(Exception e){
e.printStackTrace();
}
}
}public class Counter {
private static Counter instance;
public static Counter getInstance(){
if(instance==null){
instance = new Counter();
}
return instance;
}
public synchronized void lock(String locker){
System.out.println(locker);
while(true){

}
}
}public class Main {
public static void main(String[] args) throws InterruptedException{
Handel1 h1 = new Handel1();
h1.start();
Handel2 h2 = new Handel2();
h2.start();
}
}小弟有如下疑问, 请大家帮忙, 谢谢!1.据说方法加锁的原理是对象加锁, 线程Handel1调用lock()时, 持有了Counter实例(instance)的对象锁, 此时Handel1不执行完lock(), 线程Handel2是不能调用lock()的, 可为什么从打印的结果来看却不是这样呢?
      
2.假设Counter不是单例模式, 如果Handel1, Handel2持有了两个不同的Counter实例, 则两个线程可同时调用lock() 对吗?
      
3.为什么Object的wait()要放在synchronized块中执行?