Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int nThreads = Runtime.getRuntime().availableProcessors(); // Number of threads
- Worker[] workers = new Worker[nThreads]; // Those threads, Worker is just a thread class that can run incoming tasks
- ...
- Worker getWorker(String dispatchId) { // Get a thread for this Task
- return workers[(dispatchId.hashCode() & Integer.MAX_VALUE) % nThreads];
- }
- String dispatchId = 'SomePrefix' + counter.next()
- ConcurrentMap<String, CompletableFuture<Void>> dispatchQueues = ...
- public CompletableFuture<Void> dispatch(String queueName, Runnable task) {
- return dispatchQueues.compute(queueName, (k, queue) -> {
- return (queue == null)
- ? CompletableFuture.runAsync(task)
- : queue.thenRunAsync(task);
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement