多くの並列呼び出しを実行するテストをAPIに書きたいと思います。executorThreadsPoolの代わりにjavaパラレルストリームを使用する方法は?
devicesList.parallelStream()
.map(device -> do something)
私はjava8並列ストリームでそれを作成している可能性があり:
を私は1つのデバイス上でそれを行うことができますどのように
ExecutorService executor = Executors.newCachedThreadPool();
final int numOfUsers = 10;
for (int i = 0; i < numOfUsers; i++) {
executor.execute(() -> {
final Device device1 = getFirstDevice();
final ResponseDto responseDto = devicesServiceLocal.acquireDevice(device1.uuid, 4738);
if (responseDto.status == Status.SUCCESS)
{
successCount.incrementAndGet();
}
});
}
私はこのように、私はexecutorThreadsPoolを使用してそれを行うことができます知っていますか?
意味同じデバイスを取得するためのコールがほんの少ししか必要ありません。このような
何か:
{{device}}.parallelStream().execute(myAction).times(10)
すべてのストリームを使用する必要はありません。あなたのコードの代わりに 'コレクション。 nCopies(numOfUsers、() - > {/ *あなたのコード* /})forEach(executor :: execute);または類似の構文... –
Holger