我正在使用ScheduledThreadPoolExecutor对象安排任务.我使用以下方法:
public ScheduledFuture<?> schedule(Runnable command, long delay,TimeUnit unit)
Run Code Online (Sandbox Code Playgroud)
并将延迟设置为30秒(延迟= 30,000,单位= TimeUnit.MILLISECONDS).有时我的任务会立即发生,有时需要70秒.
我相信ScheduledThreadPoolExecutor使用CPU特定的时钟.当我运行测试比较System.currentTimeMillis(),System.nanoTime()[具体是CPU]时,我看到以下内容
时间表:1272637682651ms,7858346157228410ns
执行:1272637682667ms,7858386270968425ns
差异是16ms但是4011374001ns(或40,113ms)
因此看起来两个CPU时钟之间的差异为40秒
我如何在Java代码中解决此问题?不幸的是,这是一台客户端机器,我无法修改他们的系统.
如果我在某个集合结构中有一个链接节点,我真的不希望它的下一个链接是AtomicReference(我需要原子CAS更新)所以我将它声明为:
@volatile var next: Node[A] = _n
Run Code Online (Sandbox Code Playgroud)
然后在同伴声明:
val updater = AtomicReferenceFieldUpdater.newUpdater(classOf[Link[_]], classOf[Node[_]], "n")
def cas[A](target: Link[A], old: Node[A], newNode: Node[A]) = updater.compareAndSet(target, old, newNode);
Run Code Online (Sandbox Code Playgroud)
在运行时,我收到以下错误:
java.lang.RuntimeException: java.lang.IllegalAccessException:
Class concurrent.Link$ can not access a member of class concurrent.Link
with modifiers "private volatile"
at java.util.concurrent.atomic.AtomicReferenceFieldUpdater$AtomicReferenceFieldUpdaterImpl.<init>(AtomicReferenceFieldUpdater.java:189)
at java.util.concurrent.atomic.AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdater.java:65)
at concurrent.Link$.<init>(Link.scala:106)
...
Run Code Online (Sandbox Code Playgroud)
因此,在运行时,伴随对象concurrent.Link$不是,concurrent.Link并且不同的类不能访问另一个的私有成员.
但是,如果我 javap -p concurrent.Link
我明白了:
Compiled from "Link.scala"
public final class concurrent.Link implements concurrent.Node,scala.ScalaObject,scala.Product,java.io.Serializable{
private final java.lang.Object value;
private volatile com.atlassian.util.scala.concurrent.Node node;
public static final boolean …Run Code Online (Sandbox Code Playgroud) 根据Goetz在他的书JCIP中的说法:
因为每个线程都有自己的中断策略,所以除非知道中断对该线程的意义,否则不应该中断线程.
为什么Java语言提供了一种public interrupt ()方法?这是一个设计缺陷吗?谁或什么应该打断一个线程呢?
我已经阅读了博客,但我不确定他的结论是否正确:
http://www.javacodegeeks.com/2010/09/java-best-practices-queue-battle-and.html#ixzz1seaiSLwp
他说:从提供的性能结果可以看出,LinkedBlockingQueue实现了最佳的组合(添加和删除元素)性能结果,应该是实现生产者 - 消费者schenarios的头号候选者.
我想知道,如果我不在我的代码中使用锁定,那么它会不会更快?
那么为什么LinkedBlockingQueue比无锁队列(ConcurrentLinkedQueue)更快?
谢谢 !
据说,这ReentrantReadWriteLock是针对一位作家和多位读者的。
但是,读取器应该等待,直到缓冲区中存在一些数据为止。
那么,要锁定什么?
我创建了并发对象,如下所示:
private final ReentrantReadWriteLock rwl = new ReentrantReadWriteLock();
protected final Lock readLock = rwl.readLock();
protected final Lock writeLock = rwl.writeLock();
protected final Condition hasData = writeLock.newCondition();
Run Code Online (Sandbox Code Playgroud)
现在在写方法中,我这样做:
writeLock.lock();
// writing first portion and updating variables
hasData.signalAll();
// if required then writing second portion and updating variables
hasData.signalAll();
Run Code Online (Sandbox Code Playgroud)
但是如何写一个读者呢?它应该只获得readLock吗?但是它如何等待信号呢?如果还要求a,writeLock那么读/写锁定的最高权限在哪里?
如果必需的变量仅受的保护,如何确保它们在读取期间不会改变writeLock?
问题不匹配
这是关于的问题ReentrantReadWriteLock。
java concurrency reentrantreadwritelock java.util.concurrent
请多解释一下合同.我无法弄清楚两个锁是否以ReentrantReadWriteLock某种方式包含在内?或者这些只是一捆两个正常的锁?
如果您的资源一次只能访问一次,您可以使用大小为1的信号量,或者只使用一个ReentrantLock实例?
使一方或另一方更好的决定有什么微妙的区别?
我试图阅读同步队列的实现
对我来说并不是那么简单.它似乎使用链接列表,其中每个节点都与一个线程相关联.
核心部分使用旋转循环等待将任务放入队列中.
我想知道为什么使用旋转循环代替类似的东西wait/notify?
现在,由于这个恒定的自旋循环,其中一个核心消失了,对吧?
我试图理解这一点,并粗略地了解同步队列的设计
更新令
我不安的是服务员线程如何启动/停止.
java concurrency multithreading nonblocking java.util.concurrent
我注意到,有否在Java中AtomicBooleanArray数据类型相似AtomicIntegerArray.虽然我可以使用AtomicBoolean []来满足我当前的需求,但我很想知道为什么AtomicBooleanArray不是库的一部分.
任何想法都将非常感激.
谢谢
我在阅读JDK ConcurrentLinkedQueue时发现UNSAFE.compareAndSwapObject非常奇怪.(CLQ类是来自ConcurrentLinkedQueue的副本,以便于调试......)
当我向ConcurrentLinkedQueue提供第一项时.
在
p.casNext(null, newNode)
head == tail == p == t ref之前的同一个对象,就像这样.
之后进入casNext
UNSAFE.compareAndSwapObject(this, nextOffset, cmp, val);
p.next按预期引用newNode,就像这样.
走出去提供
一切都变得奇怪......我可以理解为什么p.next ref伤口改为p,如何自动引导newNode ...
代码:ConcurrentLinkedQueue.class offer()
public boolean offer(E e) {
checkNotNull(e);
final Node<E> newNode = new Node<E>(e);
for (Node<E> t = tail, p = t;;) {
Node<E> q = p.next;
if (q == null) {
// p is last node
if (p.casNext(null, newNode)) {
// Successful CAS is the linearization point
// for e to become …Run Code Online (Sandbox Code Playgroud)