java - 无法实例化类型 Lock

标签 java synchronization

<分区>

  1. 当使用 lock 而不是声明同步函数更公平地防止 java 中的饥饿时 它无法实例化 Lock 类型

  2. 是否需要像http://tutorials.jenkov.com/java-concurrency/starvation-and-fairness.html一样实现FairLock java的锁是否等于上面的FairLock?它说 FairLock 会降低性能,实际情况会怎样

    Lock lock = new Lock();
    

最佳答案

您可能正在尝试实例化 java.util.concurrent.locks.Lock ,这是一个接口(interface)。那当然行不通。您需要实例化一个实现,例如 ReentrantLock ,这几乎是在 Java 中执行“灵活”锁的标准方法,以防简单的监视器锁不够用。它的文档有关于公平性的说法:

The constructor for this class accepts an optional fairness parameter. When set true, under contention, locks favor granting access to the longest-waiting thread. Otherwise this lock does not guarantee any particular access order. Programs using fair locks accessed by many threads may display lower overall throughput (i.e., are slower; often much slower) than those using the default setting, but have smaller variances in times to obtain locks and guarantee lack of starvation. Note however, that fairness of locks does not guarantee fairness of thread scheduling. Thus, one of many threads using a fair lock may obtain it multiple times in succession while other active threads are not progressing and not currently holding the lock. Also note that the untimed tryLock method does not honor the fairness setting. It will succeed if the lock is available even if other threads are waiting.

关于java - 无法实例化类型 Lock,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13064485/

相关文章:

java - 浏览器不断发送 NTLM token 而不是 Kerberos - 如何解决?

java - XmlElementWrapper 和附加的奇怪值

java - 安装JHipster注册表错误: Implementation of JAXB-API has not been found on module path or classpath

java - 类的 Getter 方法是线程安全的吗?

.net - 是否有任何理由在 bool 上使用 WaitHandle 来标记取消?

java - 为什么这个 BufferedReader 返回空字符串而不是 null?

java - 使用声明式服务根据属性动态选择 OSGi 引用

mysql - 将本地数据库数据同步到远程数据库

node.js - 将批量数据插入大查询中,而不将其保留在流缓冲区中

javascript - 如何使表单提交同步?