其中1是指同步是的方法lock,即这个方法在某个时刻只有线程实例能调用。
2、 synchronized(this){
......................
}表示执行这段代码时,所有LockManager的实例都被锁定,将不能再被其它地方调用,其直到解锁,3、synchronized(locked)在执行同步代码时,只是锁定locked。下面是英文解释:
synchronized (ObjectReference) { /* Block body */ }
When a synchronized block is executed, its object is locked and it cannot be called by any other code until the lock is freed。
2、 synchronized(this){
......................
}表示执行这段代码时,所有LockManager的实例都被锁定,将不能再被其它地方调用,其直到解锁,3、synchronized(locked)在执行同步代码时,只是锁定locked。下面是英文解释:
synchronized (ObjectReference) { /* Block body */ }
When a synchronized block is executed, its object is locked and it cannot be called by any other code until the lock is freed。
因为3没有取得LockManager的同步锁反而是取得locked的同步锁
如果LockManager中有不少方法是不需要得到locked的同步锁的
那么应该是3在多线程中效率更高
也就是说可以调用如下哪些选择。A.this.wait()
B.Thread.currentThread().wait();
C.locked.wait();
>> 1是指同步是的方法lock,即这个方法在某个时刻只有线程实例能调用。
java中没有同步方法,1是2的简写。 所以1和2都是拥有this的Monitor,3是有locked的Monitor。执行效率应该是3高