Tag: 未来

如何正确处理来自ListenableFuture番石榴的exception?

我有一个库,我已经为我们的客户提供了两种方法,sync和async。 他们可以调用他们认为适合他们目的的任何方法。 executeSynchronous() – 等到我有结果,返回结果。 executeAsynchronous() – 立即返回一个Future,如果需要,可以在其他事情完成后处理。 它们将传递具有用户标识的DataKey对象。 我们将根据用户ID确定调用哪台机器。 因此,我们将使用AsyncRestTemplate对URL进行http调用,然后根据它是否成功将响应发送给它们。 以下是我的界面: public interface Client { // for synchronous public DataResponse executeSync(final DataKey key); // for asynchronous public Future executeAsync(final DataKey key); } 以下是我的实施: public class DataClient implements IClient { // does this have to be final? private final AsyncRestTemplate restTemplate = new AsyncRestTemplate(); @Override […]

如何在将来取消时在Callable中终止CXF Web服务调用

编辑 这个问题到目前为止已经经历了几次迭代,所以请随意查看修订版本以查看有关历史和所尝试事项的一些背景信息。 我正在使用CompletionService和ExecutorService以及Callable,通过CXF生成的代码同时调用几个不同的Web服务上的许多函数。这些服务都为我用于的一组信息提供不同的信息。我的项目。 但是,服务可能无法长时间响应而不会抛出exception,从而延长了等待组合信息的时间。 为了解决这个问题,我正在同时运行所有服务调用,并且在几分钟之后想要终止任何尚未完成的调用,并且最好记录哪些调用尚未完成,或者通过抛出一个详细的例外。 这里有一些高度简化的代码来说明我已经在做什么: private Callable<List> getXXXFeatures(final WiwsPortType port, final String accessionCode) { return new Callable<List>() { @Override public List call() throws Exception { List features = new ArrayList(); //getXXXFeatures are methods of the WS Proxy //that can take anywhere from second to never to return for (RawFeature raw : port.getXXXFeatures(accessionCode)) { Feature […]

Future.get(超时)的基础线程行为

我们正在使用Future以超时来完成任务。 当时间限制超过时,我们会收到TimeOutException 。 从thread dump的行为,我意识到底层线程继续。 是这样的吗? 如何处理漫游的multithreading? 如果从池中删除的线程没有IOException ,该怎么办? 如果这是真的,那么kill底层线程的方法是什么。 在我的情况下,它一直在等待外部IO 。 线程转储的一部分: Thread 29587: (state = IN_NATIVE) – java.net.SocketInputStream.socketRead0(java.io.FileDescriptor, byte[], int, int, int) @bci=0 (Compiled frame; information may be imprecise) – java.net.SocketInputStream.read(byte[], int, int) @bci=84, line=129 (Compiled frame) – java.io.BufferedInputStream.fill() @bci=175, line=218 (Compiled frame) – java.io.BufferedInputStream.read1(byte[], int, int) @bci=44, line=258 (Compiled frame) – java.io.BufferedInputStream.read(byte[], […]

Java并发:取消他们收集垃圾所需的期货?

我正在编写一些代码,我可能需要创建一个无限数量的未来对象(java.util.concurrent.Future)。 但我担心某些时候内存不足。 这里有几个问题: jvm是否知道,一旦未来完成,它就不会在任何地方被引用,因此有资格获得GC(即使创建它的线程仍在运行并运行)? 理想情况下,我不想自己跟踪这些未来。 但是,如果我确实保留这些期货的参考并定期对它们取消取消,那么它们是否可用于GC?

未来取消方法文档

根据http://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Future.html,如果调用了cancel(boolean mayInterruptIfRunning)则isDone返回true 。 此方法返回后,对isDone()的后续调用将始终返回true。 但是,任务可能正在运行且mayInterruptIfRunning设置为false 。 那么,在那次调用之后应该返回什么isDone() ? true因为取消(这是错误的)? 此外,还不清楚cancel(boolean)方法是否返回false 。 PS我正在实现一些简单的线程池,所以我inheritance了Future 。

java.util.concurrent.Future中的方法cancel()是否会阻塞?

我正在尝试在我的项目中实现Future 接口。 但看起来文档对它来说有点模糊。 从官方文档我们可以推断出: 方法cancel()不会抛出InterruptedException或ExecutionException等exception。 此外,它没有超时的变体。 所以看起来,它不应该阻止。 文档说 此方法返回后,对isDone()的后续调用将始终返回true。 但 boolean isDone()如果此任务已完成,则返回true。 因此,如果我们在任务处理时运行cancel()并且无法取消,则此方法应该等到任务完成。 哪个与1相矛盾。 cancel()的返回值描述为 返回:如果无法取消任务,则返回false,通常是因为它已经正常完成; 否则是真的 因此,如果任务正在运行并且可能被取消但不是在这个确切的时刻,我们应该返回true(我们不能声明它不能被取消)或等待(但它与1相矛盾)。 但也有一个声明 如果此方法返回true,则对isCancelled()的后续调用将始终返回true。 但 boolean isCancelled()如果此任务在正常完成之前被取消,则返回true。 当我们在任务运行时运行cancel()并且不能说任务是否可以被取消时(因为在这种情况下cancel()应该返回true,但是isCancelled()应该返回false),这与3相矛盾。 看起来很久以前这个API已经被解除了,并且这些不一致不应该出现在文档中。 但那里有。 我不明白了吗?

可调用执行期间的exception

我有以下Callable: public class Worker implements Callable{ @Override public Boolean call(){ boolean success=true; //do Something return success; } } 现在我正在执行它: Worker worker – new Worker(); Future submit = executor.submit(worker); 我正在以一种hashMap的forms存储提交,以便在代码中的某个地方执行某些操作。 我怎么知道worker.call()函数中是否发生了exception? 如果出现某种exception, submit.isCancelled()返回true,如果一切正常,则返回false吗?

‘CompletionStage’和’CompletableFuture’有什么区别?

我在每一个中都看到了一个例子,但我需要确切地知道深层有什么区别,因为有时候我觉得我可以用它们来得到相同的结果,所以我想知道这样我就可以选择正确的一? 使用它们各有什么好处? 像这个例子一样,两个都有效: public CompletionStage getNextQueryUUID() { return CompletableFuture.supplyAsync(() -> { String nextId = dbRequestService.getNextRequestQueryUUID(); return ok(nextId); }, executor); } public CompletableFuture getNextQueryUUID() { return CompletableFuture.supplyAsync(() -> { String nextId = dbRequestService.getNextRequestQueryUUID(); return ok(nextId); }, executor); } 此示例在Play framework运行。

unit testing在调试模式下成功,但在正常运行时失败

为什么我的unit testing在调试模式下成功但在正常运行时失败? public class ExecutorServiceTest extends MockitoTestCase{ private int numThreads; private ExecutorService pool; private volatile boolean interruptedBitSet; @Override public void setUp() { numThreads = 5; pool = Executors.newFixedThreadPool(numThreads); } class TaskChecksForInterruptedBit implements Callable { @Override public String call() throws Exception { interruptedBitSet = false; while (!Thread.currentThread().isInterrupted()) { } interruptedBitSet = Thread.currentThread().isInterrupted(); return “blah”; } } […]

什么是等待可完成的未来线程完成的推荐方法

我正在使用CompletableFuture ,如下面的代码所示。 但关于我应该等到所有可运行完成的方式,我找到了两种方法,我不知道它们之间的区别,哪一种是最佳实践? 它们如下: 代码 : this.growSeedFutureList = CompletableFuture.runAsync(new GrowSeedSERun(this.saliencyMat, this.seedXY, this.seedVal), this.growSeedExecutor); this.growSeedFutureList = CompletableFuture.runAsync(new GrowSeedNWRun(this.saliencyMat, this.seedXY, this.seedVal), this.growSeedExecutor); this.growSeedFutureList = CompletableFuture.runAsync(new GrowSeedNERun(this.saliencyMat, this.seedXY, this.seedVal), this.growSeedExecutor); this.growSeedFutureList = CompletableFuture.runAsync(new GrowSeedSWRun(this.saliencyMat, this.seedXY, this.seedVal), this.growSeedExecutor); 第一种等待所有runnables完成的方法 : this.growSeedExecutor.shutdown(); this.growSeedExecutor.awaitTermination(1, TimeUnit.DAYS); 等待所有runnables完成的第二种方法 : CompletableFuture.allOf(this.growSeedFutureList).join(); 请让我知道推荐哪一个。