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
...