不會不工作的方法,我試圖整合阻擋消費者在反應堆鋁SR1一個通量用戶。我想使用並行的調度程序來同時執行阻塞操作。通量的publishOn預期
我已經實現一個主類來描述我的意圖:
package etienne.peiniau;
import org.reactivestreams.Subscriber;
import org.reactivestreams.Subscription;
import reactor.core.publisher.Flux;
import reactor.core.scheduler.Schedulers;
import reactor.util.function.Tuple2;
public class Main {
public static void main(String[] args) throws InterruptedException {
Flux.just(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20)
.elapsed()
.publishOn(Schedulers.parallel())
.subscribe(new Subscriber<Tuple2<Long, Integer>>() {
@Override
public void onSubscribe(Subscription subscription) {
System.out.println("[" + Thread.currentThread().getName() + "] Subscription");
subscription.request(Long.MAX_VALUE);
}
@Override
public void onNext(Tuple2<Long, Integer> t2) {
System.out.println("[" + Thread.currentThread().getName() + "] " + t2);
try {
Thread.sleep(1000); // long operation
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@Override
public void onError(Throwable throwable) {
System.err.println("[" + Thread.currentThread().getName() + "] Error: " + throwable.getMessage());
}
@Override
public void onComplete() {
System.out.println("[" + Thread.currentThread().getName() + "] Complete");
}
});
// Waiting for the program to complete
System.out.println("[" + Thread.currentThread().getName() + "] Main");
Thread.sleep(100000);
}
}
這段代碼的輸出如下:
[main] Subscription
[main] Main
[parallel-1] [3,1]
[parallel-1] [1000,2]
[parallel-1] [1001,3]
[parallel-1] [1000,4]
[parallel-1] [1000,5]
[parallel-1] [1000,6]
[parallel-1] [1001,7]
[parallel-1] [1000,8]
[parallel-1] [1000,9]
[parallel-1] [1000,10]
[parallel-1] [1000,11]
[parallel-1] [1001,12]
[parallel-1] [1000,13]
[parallel-1] [1000,14]
[parallel-1] [1000,15]
[parallel-1] [1000,16]
[parallel-1] [1001,17]
[parallel-1] [1000,18]
[parallel-1] [1000,19]
[parallel-1] [1000,20]
[parallel-1] Complete
我的問題是,長時間操作總是執行上線程並行-1和每1秒。
我試着手動增加並行度或使用彈性調度程序,但結果是一樣的。
我在想,publishOn方法用於該用途的情況下,被專門設計的。你能否告訴我我是否誤解了某些東西?
@etiennepeiniau我猜在簡單的Flux中,它不是「並行==更多的線程」。我將編輯我的答案並添加一個示例。 –