Advertisement
Guest User

Untitled

a guest
Apr 27th, 2015
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.71 KB | None | 0 0
  1. int nThreads = Runtime.getRuntime().availableProcessors(); // Number of threads
  2. Worker[] workers = new Worker[nThreads]; // Those threads, Worker is just a thread class that can run incoming tasks
  3. ...
  4. Worker getWorker(String dispatchId) { // Get a thread for this Task
  5. return workers[(dispatchId.hashCode() & Integer.MAX_VALUE) % nThreads];
  6. }
  7.  
  8. String dispatchId = 'SomePrefix' + counter.next()
  9.  
  10. ConcurrentMap<String, CompletableFuture<Void>> dispatchQueues = ...
  11.  
  12. public CompletableFuture<Void> dispatch(String queueName, Runnable task) {
  13. return dispatchQueues.compute(queueName, (k, queue) -> {
  14. return (queue == null)
  15. ? CompletableFuture.runAsync(task)
  16. : queue.thenRunAsync(task);
  17. });
  18. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement