Могу ли я отправить заявку одному и тому же однопоточному исполнителю несколько раз?JAVA

Программисты JAVA общаются здесь
Ответить Пред. темаСлед. тема
Anonymous
 Могу ли я отправить заявку одному и тому же однопоточному исполнителю несколько раз?

Сообщение Anonymous »


У меня есть блок кода, который нужно запускать параллельно в разных потоках, используя

Код: Выделить всё

CompletableFuture
and

Код: Выделить всё

ExecutorService
. The block of code makes network requests (Using RetroFit), and waits for each network request to finish before going on to make the next request, etc. Can I call

Код: Выделить всё

SingleThreadExecutor#submit()
multiple times, passing around that executor?
I want to ensure all parallel runs of this block of code get their own thread and run all their operations on that thread.
Basically the block of code should act like synchronous code, but have multiple instance of this block running in parallel, each instance on it's own thread.
Currently, the program just idles when we make a call from the and I suspect it has to do with passing around the same

Код: Выделить всё

SingleThreadExecutor
but am a bit stuck.

Код: Выделить всё

ArrayList futures = new ArrayList();

for (int i = 0; i < SIZE; i++) {
ExecutorService executor = Executors.newSingleThreadExecutor();

CompletableFuture future = CompletableFuture.runAsync(() -> {
Future stringListFuture = myStringService.getStringList(executor);
// We begin stalling once the next line gets hit.
List stringList = stringListFuture.get();

Future integerDataFuture = myIntegerService.getInteger(executor);
Integer integerData = integerDataFuture.get();

executor.shutdown();
}, executor);

futures.add(future);
}

CompletableFuture completableFutures = CompletableFuture.allOf(futures.toArray(new CompletableFuture[0]));
completableFutures.join();

// Do something after all blocks of code have finished running in parallel.

Код: Выделить всё

class MyStringFuture {
public Future getStringList(ExecutorService executor) {

// Could these nested uses of the same executor be the problem?
Future doubeDataFuture = doubleDataService.getFuture();
Double doubleData = doubeDataFuture.get(executor);

Call data = retrofit.create(Api.class).getData(doubleData);

return executor.submit(() -> data.execute().body());
}
}


Источник: https://stackoverflow.com/questions/781 ... iple-times
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение
  • Java's Fork/присоединение к исполнителю - когда использовать что?
    Anonymous » » в форуме JAVA
    0 Ответы
    2 Просмотры
    Последнее сообщение Anonymous
  • Показывать кнопку «Отправить заявку» после отправки WPForm.
    Гость » » в форуме Jquery
    0 Ответы
    33 Просмотры
    Последнее сообщение Гость
  • Показывать кнопку «Отправить заявку» после отправки WPForm.
    Anonymous » » в форуме Jquery
    0 Ответы
    31 Просмотры
    Последнее сообщение Anonymous
  • Pywinauto Отправить ключ в заявку
    Anonymous » » в форуме Python
    0 Ответы
    4 Просмотры
    Последнее сообщение Anonymous
  • С помощью Spring Boot 3 и Spring Security 6 вы можете добавить несколько типов безопасности к одному и тому же маршруту?
    Anonymous » » в форуме JAVA
    0 Ответы
    87 Просмотры
    Последнее сообщение Anonymous

Вернуться в «JAVA»