Вся проблема может быть решена с помощью двух экземпляров ExecutorSevice .
Вам вообще не нужно создавать Thread
.Просто имейте фиксированный пул потоков на одной стороне (A) и отправляйте задачи на однопоточную одну на другой стороне (B).
Простой пример:
public static void main(final String[] args) throws InterruptedException {
final ExecutorService parallel = Executors.newFixedThreadPool(10);
final ExecutorService single = Executors.newSingleThreadExecutor();
final List<Callable<Void>> parallelTasks = IntStream.range(0, 10)
.mapToObj(i -> (Callable<Void>) () -> {
LockSupport.parkNanos(Duration.ofMillis(100).toNanos());
System.out.printf("%s parallel %s%n", Thread.currentThread().getName(), i);
single.submit(() -> {
System.out.printf("%s synchronous %s%n", Thread.currentThread().getName(), i);
});
return null;
}).collect(toList());
parallel.invokeAll(parallelTasks);
parallel.shutdown();
parallel.awaitTermination(1, TimeUnit.DAYS);
single.shutdown();
single.awaitTermination(1, TimeUnit.DAYS);
}
Вывод:
pool-1-thread-1 parallel 0
pool-1-thread-4 parallel 3
pool-1-thread-3 parallel 2
pool-1-thread-2 parallel 1
pool-1-thread-9 parallel 8
pool-1-thread-6 parallel 5
pool-1-thread-8 parallel 7
pool-1-thread-7 parallel 6
pool-1-thread-5 parallel 4
pool-2-thread-1 synchronous 3
pool-1-thread-10 parallel 9
pool-2-thread-1 synchronous 1
pool-2-thread-1 synchronous 8
pool-2-thread-1 synchronous 2
pool-2-thread-1 synchronous 5
pool-2-thread-1 synchronous 7
pool-2-thread-1 synchronous 0
pool-2-thread-1 synchronous 6
pool-2-thread-1 synchronous 4
pool-2-thread-1 synchronous 9