java.util.concurrent.locks.ReentrantLock.isFair()方法的使用及代码示例

x33g5p2x  于2022-01-28 转载在 其他  
字(6.5k)|赞(0)|评价(0)|浏览(270)

本文整理了Java中java.util.concurrent.locks.ReentrantLock.isFair方法的一些代码示例,展示了ReentrantLock.isFair的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ReentrantLock.isFair方法的具体详情如下:
包路径:java.util.concurrent.locks.ReentrantLock
类名称:ReentrantLock
方法名:isFair

ReentrantLock.isFair介绍

[英]Returns true if this lock has fairness set true.
[中]如果此锁的公平性设置为true,则返回true。

代码示例

代码示例来源:origin: alibaba/druid

public boolean isUseUnfairLock() {
  return lock.isFair();
}

代码示例来源:origin: alibaba/druid

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();
    }
  }
}

代码示例来源:origin: alibaba/druid

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;
}

代码示例来源:origin: com.alibaba/druid

public boolean isUseUnfairLock() {
  return lock.isFair();
}

代码示例来源:origin: com.alibaba/druid

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();
    }
  }
}

代码示例来源:origin: com.alibaba/druid

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;
}

代码示例来源:origin: harbby/presto-connectors

/**
 * Returns whether this monitor is using a fair ordering policy.
 */
public boolean isFair() {
 return lock.isFair();
}

代码示例来源:origin: org.sonatype.sisu/sisu-guava

/**
 * Returns whether this monitor is using a fair ordering policy.
 */
public boolean isFair() {
 return lock.isFair();
}

代码示例来源:origin: io.prestosql.hive/hive-apache-jdbc

/**
 * Returns whether this monitor is using a fair ordering policy.
 */
public boolean isFair() {
 return lock.isFair();
}

代码示例来源:origin: io.prestosql.hadoop/hadoop-apache

/**
 * Returns whether this monitor is using a fair ordering policy.
 */
public boolean isFair() {
 return lock.isFair();
}

代码示例来源:origin: org.hudsonci.lib.guava/guava

/**
 * Returns whether this monitor is using a fair ordering policy.
 */
public boolean isFair() {
 return lock.isFair();
}

代码示例来源:origin: com.facebook.presto.hive/hive-apache-jdbc

/**
 * Returns whether this monitor is using a fair ordering policy.
 */
public boolean isFair() {
 return lock.isFair();
}

代码示例来源:origin: loveincode/Java-Multi-thread-Programming

public void serviceMethod() {
  try {
    lock.lock();
    System.out.println("公平锁情况:" + lock.isFair());
  } finally {
    lock.unlock();
  }
}

代码示例来源:origin: dunwu/javacore

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();
    }
  }
}

代码示例来源:origin: kabutz/javaspecialists

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);
  }
}

代码示例来源:origin: dunwu/javacore

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();
    }
  }
}

代码示例来源:origin: dunwu/javacore

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() + " 获取锁失败");
    }
  }
}

代码示例来源:origin: dunwu/javacore

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();
    }
  }
}

相关文章