I'm wondering what would be the best approach for re-using threads. I know that when you create an ActorThread each binded actorref created from that ActorThread will use the same Thread. So if I want to create multiple actor refs, each using a different thread, I must stop teh actorThread such that it can server anotehr actor. Is that right?
See the following example. Replace my for loop with a business logic running behind a thread pool (e.g. a web-app handler thread pool for example). Does the below sound like a right best-practice? Or maybe calling the stop thread API all the time is costly?
public class ActorsTest {
private static final int NUM_OF_ITER = 100;
@Test
public void actorsTest() throws Exception{
CountDownLatch countDownLatch = new CountDownLatch(NUM_OF_ITER);
FooImpl foo = new FooImpl();
Actors actors = new MultiThreadedActors(
Executors.newFixedThreadPool(3),
new DynamicEventizerProvider(),
new CrashEarlyFailureHandler(),
new NullMessageListener()
);
for (int i = 1; i <= NUM_OF_ITER; i++) {
ActorThread actorThread = actors.startActorThread();
ActorRef<Foo> fooActorRef = actorThread.bindActor(Foo.class, foo);
fooActorRef.tell().print("message: " + i, countDownLatch);
actorThread.stop();
}
countDownLatch.await();
}
public static interface Foo {
void print(String msg, CountDownLatch countDownLatch);
}
public static class FooImpl implements Foo {
@Override
public void print(String msg, CountDownLatch countDownLatch) {
System.out.println("[" + Thread.currentThread().getName() + ":"+Thread.currentThread().getId()+"] " + msg);
countDownLatch.countDown();
}
}
}