我需要找到一种从Future转换为ListenableFuture的方法.目前我正在使用一个返回Future的服务,但我需要连接一个监听器.我无法更改服务界面,因为它不属于我.
有一个简单的方法吗?
我已经阅读了番石榴文档,但我仍然找不到办法.
您认为在Java中实现发布/获取对的获取部分的最佳方法是什么?
我正在尝试使用经典的发布/获取语义模拟我的应用程序中的一些操作(没有StoreLoad和没有跨线程的顺序一致性).
有几种方法可以实现JDK中存储释放的粗略等效.java.util.concurrent.Atomic*.lazySet()并且sun.misc.Unsafe.putOrdered*()最常被引用的方法是做到这一点.然而,没有明显的方法来实现负载获取.
JDK API主要允许在内部lazySet()使用volatile变量,因此它们的存储版本与易失性负载配对.理论上,易失性负载应该比负载获取更昂贵,并且在前面的存储释放的上下文中不应该提供比纯粹的负载获取更多的东西.
sun.misc.Unsafe虽然这些获取方法是针对即将推出的VarHandles API计划的,但并未提供方法的getAcquire()*等效putOrdered*()方法.
听起来像它会起作用的东西是明显的负荷,接着是sun.misc.Unsafe.loadFence().有点令人不安的是,我还没有在其他任何地方看到过这种情况.这可能与它是一个非常丑陋的黑客的事实有关.
PS我很清楚JMM没有涵盖这些机制,它们不足以维持顺序一致性,并且它们创建的动作不是同步动作(例如我理解它们例如打破了IRIW).我也理解,提供的商店版本Atomic*/Unsafe通常用于急切地将引用或生产者/消费者场景中的空白作为一些重要索引的优化消息传递机制.
java concurrency multithreading memory-model memory-barriers
我在那里看到对预先分配的JVM异常的引用: - http://www.oracle.com/technetwork/java/javase/relnotes-139183.html - http://dev.clojure.org/display/community/Project+创意+ 2016
但寻找我只看到有关丢失堆栈跟踪的信息.什么是JVM分配的异常?这似乎是一种优化.
它是如何工作的,它有什么权衡取舍?
我有以下代码:
public class MyApp {
public static void main(String[] args) throws InterruptedException {
SharedResource sharedResource = new SharedResource();
Runnable first = () -> {
sharedResource.increment(10000);
};
Runnable second = () -> {
sharedResource.increment(10000);
};
Thread thread = new Thread(first, "FirstThread");
Thread thread2 = new Thread(second, "SecondThread");
thread.start();
thread2.start();
thread.join();
thread2.join();
System.out.println("The value of counter is " + sharedResource.getCounter());
}
}
Run Code Online (Sandbox Code Playgroud)
使用这个类:
public class SharedResource {
private int counter;
public void increment(int times) {
for (int x=1; x<=times;x++) {
counter++;
// …Run Code Online (Sandbox Code Playgroud) java ×4
concurrency ×3
futuretask ×1
guava ×1
jvm ×1
jvm-hotspot ×1
memory-model ×1
performance ×1