[英]How to wrap Thread with ReentrantLock into CompletableFuture call?
This is my current implementation which handles different file read/save operations consecutively: 这是我当前的实现,它连续处理不同的文件读取/保存操作:
public void runThread(MyThreadImpl myThreadImpl) {
synchronized (this) {
this.myThreadImpl = myThreadImpl;
notify();
}
}
synchronized public void run() {
while (true)
try {
wait();
Global.myReentrantLock.lock();
try {
try {
myThreadImpl.call();
} catch (FileException e) {
// trace e
} catch (RuntimeException e) {
// trace e
} catch (Exception e) {
// trace e
}
} finally {
Global.myReentrantLock.unlock();
}
} catch (InterruptedException e) {
// trace e
} catch (Exception e) {
// trace e
}
}
I have a problem that I don't wait for thread result before performing another operation, and I've come to a case where that is necessary. 我有一个问题,我不等待线程结果再执行其他操作,因此我认为有必要这样做。
Since I'm using Java 8, I wanted to wrap this in a CompletableFuture. 由于我使用的是Java 8,因此我想将其包装在CompletableFuture中。 How can I do this with my current implementation? 如何在当前的实现中执行此操作?
You could do the following: 您可以执行以下操作:
this.myThreadImpl
) that is updated once the lock is free, you could use a queue. 您可以使用队列,而不是将要完成的下一个作业存储为在this.myThreadImpl
锁后进行更新的单个引用( this.myThreadImpl
)。 CompletableFuture
is created, and a reference to it is returned to the caller. 添加新作业后,将创建一个新的CompletableFuture
,并将对它的引用返回给调用方。 Updating your code, and assuming queue
is a blocking queue of type Queue<Pair<CompletableFuture<Void>, MyThreadImpl>>
, you would have: 更新代码,并假设queue
是类型为Queue<Pair<CompletableFuture<Void>, MyThreadImpl>>
的阻塞队列,您将具有:
/**
* @return a Future that will complete once the passed MyThreadImpl has been run.
*/
public CompletableFuture<Void> runThread(MyThreadImpl myThreadImpl) {
Pair<CompletableFuture<Void>, MyThreadImpl> p =
new Pair<>(new CompletableFuture<>(),myThreadImpl);
queue.add(p);
return p.left;
}
public void run() {
while (true) {
try {
Pair<CompletableFuture<MyThreadImpl>, MyThreadImpl> p =
queue.take(); // will block until a job is added
try {
p.right.call();
p.left.complete(null); // Future<Void> can only be completed with null. Alternatively, it could be completed with a relevant result.
} catch (Exception e) {
p.left.completeExceptionally(e);
}
} catch (InterruptedException e) {
// trace e
}
}
}
Here, Pair
just needs to be a pair-like pojo. 在这里, Pair
只是需要像对一样的pojo。 (It could be apache commons's ImmutablePair
, for example.) (例如,它可能是apache commons的ImmutablePair
。)
Blocking queues are generally useful when stuff needs to be processed: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/BlockingQueue.html 当需要处理东西时,阻塞队列通常很有用: https : //docs.oracle.com/javase/8/docs/api/java/util/concurrent/BlockingQueue.html
Also, have you looked at ExecutorService
? 另外,您是否看过ExecutorService
? You could use one that is based on a single thread to execute jobs in a serial way: it's submit(Callable<> task)
method is quite like runThread()
defined above, as it returns a Future<Void>
that will tell you when the task is done. 您可以使用基于单个线程的线程以串行方式执行作业:它的submit(Callable<> task)
方法与上面定义的runThread()
十分相似,因为它返回一个Future<Void>
,它将告诉您何时任务完成了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.