} else { // TODO 如果同步标志位是1,并且锁的拥有者是当前线程的话,则可以设置重入,但本方法暂未实现 if (1 == state && Thread.currentThread() == exclusiveOwnerThread) { // 进行设置重入锁 }
System.out.println(Thread.currentThread() + " lock fail ! If the owner of the lock is the current thread," + " the reentrant lock needs to be set;else Adds the current thread to the blocking queue .");
/** * 释放锁 * * @return */ publicfinalbooleanunlock(){ // 判断锁的拥有者是否为当前线程 if (Thread.currentThread() != exclusiveOwnerThread) { thrownew IllegalMonitorStateException("Lock release failed ! The owner of the lock is not " + "the current thread."); } // 将同步标志位设置为0,初始未加锁状态 state = 0; // 将独占锁的拥有者设置为 null exclusiveOwnerThread = null;
System.out.println(Thread.currentThread() + " Release the lock successfully, and then wake up " + "the thread node in the blocking queue ! state:" + state);
} else { // TODO 如果同步标志位是1,并且锁的拥有者是当前线程的话,则可以设置重入,但本方法暂未实现 if (1 == state && Thread.currentThread() == exclusiveOwnerThread) { // 进行设置重入锁 }
System.out.println(Thread.currentThread() + " lock fail ! If the owner of the lock is the current thread," + " the reentrant lock needs to be set;else Adds the current thread to the blocking queue .");
// 线程被唤醒后会执行此处,并且继续执行此 while 循环 System.out.println(Thread.currentThread() + " The currently blocking thread is awakened !"); } } }
/** * 释放锁 * * @return */ publicfinalbooleanunlock(){ if (Thread.currentThread() != exclusiveOwnerThread) { thrownew IllegalMonitorStateException("Lock release failed ! The owner of the lock is not " + "the current thread."); } // 将同步标志位设置为0,初始未加锁状态 state = 0; // 将独占锁的拥有者设置为 null exclusiveOwnerThread = null;
System.out.println(Thread.currentThread() + " Release the lock successfully, and then wake up " + "the thread node in the blocking queue ! state:" + state);