[英]Wrapping an asynchronous computation into a synchronous (blocking) computation
類似問題:
我有一個對象,我想向庫客戶端(尤其是腳本客戶端)公開一個方法,例如:
interface MyNiceInterface
{
public Baz doSomethingAndBlock(Foo fooArg, Bar barArg);
public Future<Baz> doSomething(Foo fooArg, Bar barArg);
// doSomethingAndBlock is the straightforward way;
// doSomething has more control but deals with
// a Future and that might be too much hassle for
// scripting clients
}
但我可用的原始“東西”是一組事件驅動類:
interface BazComputationSink
{
public void onBazResult(Baz result);
}
class ImplementingThing
{
public void doSomethingAsync(Foo fooArg, Bar barArg, BazComputationSink sink);
}
其中,ImplementingThing 接受輸入,做一些神秘的事情,比如在任務隊列上排隊,然后當結果出現時, sink.onBazResult()
在一個線程上被調用,該線程可能是也可能不是與 ImplementingThing.doSomethingAsync() 相同的線程被稱為。
有沒有一種方法可以使用我擁有的事件驅動函數以及並發原語來實現 MyNiceInterface 以便腳本客戶端可以愉快地等待阻塞線程?
編輯:我可以為此使用FutureTask嗎?
使用您自己的 Future 實現:
public class BazComputationFuture implements Future<Baz>, BazComputationSink {
private volatile Baz result = null;
private volatile boolean cancelled = false;
private final CountDownLatch countDownLatch;
public BazComputationFuture() {
countDownLatch = new CountDownLatch(1);
}
@Override
public boolean cancel(final boolean mayInterruptIfRunning) {
if (isDone()) {
return false;
} else {
countDownLatch.countDown();
cancelled = true;
return !isDone();
}
}
@Override
public Baz get() throws InterruptedException, ExecutionException {
countDownLatch.await();
return result;
}
@Override
public Baz get(final long timeout, final TimeUnit unit)
throws InterruptedException, ExecutionException, TimeoutException {
countDownLatch.await(timeout, unit);
return result;
}
@Override
public boolean isCancelled() {
return cancelled;
}
@Override
public boolean isDone() {
return countDownLatch.getCount() == 0;
}
public void onBazResult(final Baz result) {
this.result = result;
countDownLatch.countDown();
}
}
public Future<Baz> doSomething(Foo fooArg, Bar barArg) {
BazComputationFuture future = new BazComputationFuture();
doSomethingAsync(fooArg, barArg, future);
return future;
}
public Baz doSomethingAndBlock(Foo fooArg, Bar barArg) {
return doSomething(fooArg, barArg).get();
}
該解決方案在內部創建了一個 CountDownLatch,一旦收到回調就會清除它。 如果用戶調用 get,則 CountDownLatch 用於阻塞調用線程,直到計算完成並調用 onBazResult 回調。 CountDownLatch 將確保如果在調用 get() 之前發生回調,則 get() 方法將立即返回結果。
好吧,有一個簡單的解決方案,可以執行以下操作:
public Baz doSomethingAndBlock(Foo fooArg, Bar barArg) {
final AtomicReference<Baz> notifier = new AtomicReference();
doSomethingAsync(fooArg, barArg, new BazComputationSink() {
public void onBazResult(Baz result) {
synchronized (notifier) {
notifier.set(result);
notifier.notify();
}
}
});
synchronized (notifier) {
while (notifier.get() == null)
notifier.wait();
}
return notifier.get();
}
當然,這假設您的Baz
結果永遠不會為空……
google guava 庫有一個易於使用的 SettableFuture,可以讓這個問題變得非常簡單(大約 10 行代碼)。
public class ImplementingThing {
public Baz doSomethingAndBlock(Foo fooArg, Bar barArg) {
try {
return doSomething(fooArg, barArg).get();
} catch (Exception e) {
throw new RuntimeException("Oh dear");
}
};
public Future<Baz> doSomething(Foo fooArg, Bar barArg) {
final SettableFuture<Baz> future = new SettableFuture<Baz>();
doSomethingAsync(fooArg, barArg, new BazComputationSink() {
@Override
public void onBazResult(Baz result) {
future.set(result);
}
});
return future;
};
// Everything below here is just mock stuff to make the example work,
// so you can copy it into your IDE and see it run.
public static class Baz {}
public static class Foo {}
public static class Bar {}
public static interface BazComputationSink {
public void onBazResult(Baz result);
}
public void doSomethingAsync(Foo fooArg, Bar barArg, final BazComputationSink sink) {
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(4000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Baz baz = new Baz();
sink.onBazResult(baz);
}
}).start();
};
public static void main(String[] args) {
System.err.println("Starting Main");
System.err.println((new ImplementingThing()).doSomethingAndBlock(null, null));
System.err.println("Ending Main");
}
這對於 RxJava 2.x 來說非常簡單:
try {
Baz baz = Single.create((SingleEmitter<Baz> emitter) ->
doSomethingAsync(fooArg, barArg, result -> emitter.onSuccess(result)))
.toFuture().get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
或者不使用 Lambda 符號:
Baz baz = Single.create(new SingleOnSubscribe<Baz>() {
@Override
public void subscribe(SingleEmitter<Baz> emitter) {
doSomethingAsync(fooArg, barArg, new BazComputationSink() {
@Override
public void onBazResult(Baz result) {
emitter.onSuccess(result);
}
});
}
}).toFuture().get();
更簡單:
Baz baz = Single.create((SingleEmitter<Baz> emitter) ->
doSomethingAsync(fooArg, barArg, result -> emitter.onSuccess(result)))
.blockingGet();
科特林版本:
val baz = Single.create<Baz> { emitter ->
doSomethingAsync(fooArg, barArg) { result -> emitter.onSuccess(result) }
}.blockingGet()
一個非常簡單的例子,只是為了理解CountDownLatch,沒有任何額外的代碼。
java.util.concurrent.CountDownLatch
是一種並發構造,它允許一個或多個線程等待一組給定的操作完成。
CountDownLatch
使用給定的計數進行初始化。 該計數通過調用countDown()
方法遞減。 等待此計數達到零的線程可以調用await()
方法之一。 調用await()
阻塞線程,直到計數達到零。
下面是一個簡單的例子。 在遞減器在CountDownLatch
上調用countDown()
3 次后,等待的 Waiter 從await()
調用中釋放。
您還可以提及一些TimeOut
等待。
CountDownLatch latch = new CountDownLatch(3);
Waiter waiter = new Waiter(latch);
Decrementer decrementer = new Decrementer(latch);
new Thread(waiter) .start();
new Thread(decrementer).start();
Thread.sleep(4000);
public class Waiter implements Runnable{
CountDownLatch latch = null;
public Waiter(CountDownLatch latch) {
this.latch = latch;
}
public void run() {
try {
latch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Waiter Released");
}
}
//--------------
public class Decrementer implements Runnable {
CountDownLatch latch = null;
public Decrementer(CountDownLatch latch) {
this.latch = latch;
}
public void run() {
try {
Thread.sleep(1000);
this.latch.countDown();
Thread.sleep(1000);
this.latch.countDown();
Thread.sleep(1000);
this.latch.countDown();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
如果您不想使用CountDownLatch
或您的要求與 Facebook 的功能相同但功能不同。 意味着如果正在調用一種方法,則不要調用另一種方法。
在這種情況下,您可以聲明一個
private volatile Boolean isInprocessOfLikeOrUnLike = false;
然后你可以在你的方法調用的開頭檢查,如果它是false
那么調用方法否則返回..取決於你的實現。
這是基於 Paul Wagland 的回答的更通用的解決方案:
public abstract class AsyncRunnable<T> {
protected abstract void run(AtomicReference<T> notifier);
protected final void finish(AtomicReference<T> notifier, T result) {
synchronized (notifier) {
notifier.set(result);
notifier.notify();
}
}
public static <T> T wait(AsyncRunnable<T> runnable) {
final AtomicReference<T> notifier = new AtomicReference<>();
// run the asynchronous code
runnable.run(notifier);
// wait for the asynchronous code to finish
synchronized (notifier) {
while (notifier.get() == null) {
try {
notifier.wait();
} catch (InterruptedException ignore) {}
}
}
// return the result of the asynchronous code
return notifier.get();
}
}
這是一個如何使用它的示例:
String result = AsyncRunnable.wait(new AsyncRunnable<String>() {
@Override
public void run(final AtomicReference<String> notifier) {
// here goes your async code, e.g.:
new Thread(new Runnable() {
@Override
public void run() {
finish(notifier, "This was a asynchronous call!");
}
}).start();
}
});
可以在此處找到更詳細的代碼版本: http : //pastebin.com/hKHJUBqE
編輯:與問題相關的示例是:
public Baz doSomethingAndBlock(final Foo fooArg, final Bar barArg) {
return AsyncRunnable.wait(new AsyncRunnable<Baz>() {
@Override
protected void run(final AtomicReference<Baz> notifier) {
doSomethingAsync(fooArg, barArg, new BazComputationSink() {
public void onBazResult(Baz result) {
synchronized (notifier) {
notifier.set(result);
notifier.notify();
}
}
});
}
});
}
最簡單的方法(對我有用)是
輪詢隊列(即您阻塞的位置)以獲取結果。
public Baz doSomethingAndBlock(Foo fooArg, Bar barArg) throws InterruptedException { final BlockingQueue<Baz> blocker = new LinkedBlockingQueue(); doSomethingAsync(fooArg, barArg, blocker::offer); // Now block until response or timeout return blocker.poll(30, TimeUnit.SECONDS); }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.