Hi Alessandro, KJ doesn't currently have any built-in thread pool implementation. The KJ async / Promise framework is centered around single-threaded event loops, so probably isn't what you're looking for.
I guess if I were implementing this I'd write a class that creates threads using kj::Thread, and has some sort of kj::MutexGuarded<Queue<Job>>. Each thread would run a loop where it uses MutexGuarded::when() to wait for the queue to be non-empty, pops the top job off the queue, and runs it. -Kenton On Fri, Jul 30, 2021 at 2:51 PM Alessandro Arcangeli < alessandroarcangeli...@gmail.com> wrote: > Hi there, > > I'm trying to implement a pool thread executor using KJ. > > I mean an executor that has a list of threads and sends the job to the > "most empty" one. > > Or better, when a thread finish his job (or it is blocked by an IO) it > automatically pick the next job from the executor. > > I looked up in kj's public api but found no way (also combining > executors/threads/TaskSets etc). > > Is there a way to do something like this using KJ?. > > Thanks > > *Sample Java Equivalent:* > > import java.util.concurrent.ExecutorService; > import java.util.concurrent.Executors; > > public class TestExecutor { > public static void main(String[] args) { > ExecutorService service = Executors.newFixedThreadPool(8); > > for (int i = 0; i < 100; i++) { > service.execute(() -> { > System.out.println("Running from " + > Thread.currentThread().getName()); > try { > Thread.sleep(100); > } > catch (InterruptedException e) { > } > }); > } > > service.shutdown(); > } > } > > > *Sample output:* > > Running from pool-1-thread-2 > Running from pool-1-thread-8 > Running from pool-1-thread-3 > Running from pool-1-thread-5 > Running from pool-1-thread-1 > Running from pool-1-thread-7 > Running from pool-1-thread-4 > Running from pool-1-thread-6 > Running from pool-1-thread-7 > Running from pool-1-thread-6 > Running from pool-1-thread-3 > Running from pool-1-thread-8 > ... > > -- > You received this message because you are subscribed to the Google Groups > "Cap'n Proto" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to capnproto+unsubscr...@googlegroups.com. > To view this discussion on the web visit > https://groups.google.com/d/msgid/capnproto/b331a5f4-4162-46b5-a160-ead0ffecdfddn%40googlegroups.com > <https://groups.google.com/d/msgid/capnproto/b331a5f4-4162-46b5-a160-ead0ffecdfddn%40googlegroups.com?utm_medium=email&utm_source=footer> > . > -- You received this message because you are subscribed to the Google Groups "Cap'n Proto" group. To unsubscribe from this group and stop receiving emails from it, send an email to capnproto+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/capnproto/CAJouXQnoyApjH6inTHCP%2BL151pe3bTsuxtV63AzVZQOVwBsUwA%40mail.gmail.com.