public boolean isUseUnfairLock() { return lock.isFair(); }
public void setUseUnfairLock(boolean useUnfairLock) { if (lock.isFair() == !useUnfairLock) { return; } if (!this.inited) { final ReentrantLock lock = this.lock; lock.lock(); try { if (!this.inited) { this.lock = new ReentrantLock(!useUnfairLock); this.notEmpty = this.lock.newCondition(); this.empty = this.lock.newCondition(); this.useUnfairLock = useUnfairLock; } } finally { lock.unlock(); } } }
public void setMaxWait(long maxWaitMillis) { if (maxWaitMillis == this.maxWait) { return; } if (maxWaitMillis > 0 && useUnfairLock == null && !this.inited) { final ReentrantLock lock = this.lock; lock.lock(); try { if ((!this.inited) && (!lock.isFair())) { this.lock = new ReentrantLock(true); this.notEmpty = this.lock.newCondition(); this.empty = this.lock.newCondition(); } } finally { lock.unlock(); } } if (inited) { LOG.error("maxWait changed : " + this.maxWait + " -> " + maxWaitMillis); } this.maxWait = maxWaitMillis; }
public boolean isUseUnfairLock() { return lock.isFair(); }
public void setUseUnfairLock(boolean useUnfairLock) { if (lock.isFair() == !useUnfairLock) { return; } if (!this.inited) { final ReentrantLock lock = this.lock; lock.lock(); try { if (!this.inited) { this.lock = new ReentrantLock(!useUnfairLock); this.notEmpty = this.lock.newCondition(); this.empty = this.lock.newCondition(); this.useUnfairLock = useUnfairLock; } } finally { lock.unlock(); } } }
public void setMaxWait(long maxWaitMillis) { if (maxWaitMillis == this.maxWait) { return; } if (maxWaitMillis > 0 && useUnfairLock == null && !this.inited) { final ReentrantLock lock = this.lock; lock.lock(); try { if ((!this.inited) && (!lock.isFair())) { this.lock = new ReentrantLock(true); this.notEmpty = this.lock.newCondition(); this.empty = this.lock.newCondition(); } } finally { lock.unlock(); } } if (inited) { LOG.error("maxWait changed : " + this.maxWait + " -> " + maxWaitMillis); } this.maxWait = maxWaitMillis; }
/** * Returns whether this monitor is using a fair ordering policy. */ public boolean isFair() { return lock.isFair(); }
/** * Returns whether this monitor is using a fair ordering policy. */ public boolean isFair() { return lock.isFair(); }
/** * Returns whether this monitor is using a fair ordering policy. */ public boolean isFair() { return lock.isFair(); }
/** * Returns whether this monitor is using a fair ordering policy. */ public boolean isFair() { return lock.isFair(); }
/** * Returns whether this monitor is using a fair ordering policy. */ public boolean isFair() { return lock.isFair(); }
/** * Returns whether this monitor is using a fair ordering policy. */ public boolean isFair() { return lock.isFair(); }
public void serviceMethod() { try { lock.lock(); System.out.println("公平锁情况:" + lock.isFair()); } finally { lock.unlock(); } }
public void execute() { lock.lock(); try { for (int i = 0; i < 3; i++) { System.out.println(Thread.currentThread().getName()); // 查询当前线程保持此锁的次数 System.out.println("\t holdCount: " + lock.getHoldCount()); // 返回正等待获取此锁的线程估计数 System.out.println("\t queuedLength: " + lock.getQueueLength()); // 如果此锁的公平设置为 true,则返回 true System.out.println("\t isFair: " + lock.isFair()); try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } } } finally { lock.unlock(); } } }
public static <E> ArrayBlockingQueue<E> makeManaged( ArrayBlockingQueue<E> queue) { Class<?> clazz = ArrayBlockingQueue.class; try { Field lockField = clazz.getDeclaredField("lock"); lockField.setAccessible(true); ReentrantLock old = (ReentrantLock) lockField.get(queue); boolean fair = old.isFair(); ReentrantLock lock = new ManagedReentrantLock(fair); lockField.set(queue, lock); replace(queue, clazz, "notEmpty", lock.newCondition()); replace(queue, clazz, "notFull", lock.newCondition()); return queue; } catch (IllegalAccessException | NoSuchFieldException e) { throw new IllegalStateException(e); } }
public void execute() { try { lock.lockInterruptibly(); for (int i = 0; i < 3; i++) { System.out.println(Thread.currentThread().getName()); // 查询当前线程保持此锁的次数 System.out.println("\t holdCount: " + lock.getHoldCount()); // 返回正等待获取此锁的线程估计数 System.out.println("\t queuedLength: " + lock.getQueueLength()); // 如果此锁的公平设置为 true,则返回 true System.out.println("\t isFair: " + lock.isFair()); try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } } } catch (InterruptedException e) { System.out.println(Thread.currentThread().getName() + "被中断"); e.printStackTrace(); } finally { lock.unlock(); } } }
public void execute() { if (lock.tryLock()) { try { for (int i = 0; i < 3; i++) { System.out.println(Thread.currentThread().getName()); // 查询当前线程保持此锁的次数 System.out.println("\t holdCount: " + lock.getHoldCount()); // 返回正等待获取此锁的线程估计数 System.out.println("\t queuedLength: " + lock.getQueueLength()); // 如果此锁的公平设置为 true,则返回 true System.out.println("\t isFair: " + lock.isFair()); try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } } } finally { lock.unlock(); } } else { System.out.println(Thread.currentThread().getName() + " 获取锁失败"); } } }
public void execute() { try { if (lock.tryLock(2, TimeUnit.SECONDS)) { try { for (int i = 0; i < 3; i++) { System.out.println(Thread.currentThread().getName()); // 查询当前线程保持此锁的次数 System.out.println("\t holdCount: " + lock.getHoldCount()); // 返回正等待获取此锁的线程估计数 System.out.println("\t queuedLength: " + lock.getQueueLength()); // 如果此锁的公平设置为 true,则返回 true System.out.println("\t isFair: " + lock.isFair()); try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } } } finally { lock.unlock(); } } else { System.out.println(Thread.currentThread().getName() + " 获取锁失败"); } } catch (InterruptedException e) { System.out.println(Thread.currentThread().getName() + " 获取锁超时"); e.printStackTrace(); } } }