Timer for actor in quasar

45 views
Skip to first unread message

Влад Чесноков

unread,
Sep 25, 2017, 9:44:37 AM9/25/17
to quasar-pulsar-user
This is a copy of SO question: https://stackoverflow.com/questions/46300493/timer-for-actor-in-quasar

I've an actor, which I want to perform an action with delay, i.e. something like this (pseudocode):

void onReceive(message: Message){
    if (message is DelayedAction){
        sleep(100);
        preformSomeAction();
    }
    /*...*/
}

It is prudent to wait in another thread/strand while processing the other message, so I've added an ScheduledThreadPool for an actor:

ScheduledExecutorService executor = Executors.newScheduledThreadPool(0);

void onReceive(message: Message){
    if (message is DelayedAction){
        executor.schedule(sendMessageForAction, delay, TimeUnit.MILLISECONDS);
    }
    else if (message is MessageForAction){
        preformSomeAction();
    }
    /*...*/
}

So, waiting is performed in executor thread pool, and after that a message sent to actor.self to actually perform a task.

But sometimes messages are lost (only increasing number of threads in the pool helps). Now I'm not quite sure that I'm doing a right thing: may be quasar is not ok with threadPoolExecutors and I should use some other approach to delay action perform?

What is the right approach to delayed action on message receiving?


One other option for me is to use fibers without pool:

void onReceive(message: Message){
    if (message is DelayedAction){
        new Fiber<V>() {
            @Override
            protected V run() throws SuspendExecution, InterruptedException {
                Strand.sleep(delay);
                sendMessageForAction();
            }
       }.start();
    }
    else if (message is MessageForAction){
        preformSomeAction();
    }
    /*...*/
}

I'm still not sure if it is the right way to go.

Reply all
Reply to author
Forward
0 new messages