lunicon
lunicon

Reputation: 1740

Executors with shared queue

I have one task producer and several hardware resources for execute them So, I try to create executers with shared queue

BlockingQueue<Runnable> queue;
ExecutorService executor1 = new ThreadPoolExecutor(poolSize1, poolSize1, 0L, TimeUnit.MILLISECONDS, queue, threadFactory);
ExecutorService executor2 = new ThreadPoolExecutor(poolSize2, poolSize2, 0L, TimeUnit.MILLISECONDS, queue, threadFactory);
ExecutorService executor3 = new ThreadPoolExecutor(poolSize3, poolSize3, 0L, TimeUnit.MILLISECONDS, queue, threadFactory);

and (just try) to add task to queue.put(task) instead of specific executorN.execute(task) But, execurer doesn't create thread until execute(task) call.

So, I need an implementation of Executor? which run task on any free thread of sub-execurer. Maybe you know the solution/library which can be used?

This is not a balancer. I don't care where the task will start. Let it stand in queue until it is taken by a free handler

Upvotes: 0

Views: 278

Answers (1)

Matthew I.
Matthew I.

Reputation: 1803

I would rather you implemented it manually:

    BlockingQueue<Runnable> queue = new ArrayBlockingQueue<>(500);

    IntStream.range(0, 10).forEach(i -> {
      Thread t = new Thread(() -> {
        while (true) {
          try {
            queue.take().run();
          } catch (InterruptedException e) {
            break;
          }
        }
      });
      t.setDaemon(true);
      t.setName("worker-"+i);
      t.start();
    });

    queue.add(() -> System.out.println(Thread.currentThread().getName()));
    queue.add(() -> System.out.println(Thread.currentThread().getName()));
    queue.add(() -> System.out.println(Thread.currentThread().getName()));
    queue.add(() -> System.out.println(Thread.currentThread().getName()));
    queue.add(() -> System.out.println(Thread.currentThread().getName()));

    Thread.sleep(1000);

    queue.add(() -> System.out.println(Thread.currentThread().getName()));
    queue.add(() -> System.out.println(Thread.currentThread().getName()));
    queue.add(() -> System.out.println(Thread.currentThread().getName()));
    queue.add(() -> System.out.println(Thread.currentThread().getName()));
    queue.add(() -> System.out.println(Thread.currentThread().getName()));
    queue.add(() -> System.out.println(Thread.currentThread().getName()));
    queue.add(() -> System.out.println(Thread.currentThread().getName()));
    queue.add(() -> System.out.println(Thread.currentThread().getName()));

    Thread.sleep(1000);

and output would be:

worker-0
worker-1
worker-2
worker-3
worker-4
worker-5
worker-6
worker-7
worker-8
worker-9
worker-0
worker-1
worker-2

P.S. threads will wait for a new item in the queue.

Upvotes: 2

Related Questions