Click here to Skip to main content
15,889,200 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to build a very simple, yet efficient, scheduler to which classes that implement a SchedulerListener interface (for started/finished events) register with how many seconds and/or milliseconds must the scheduler wait to notify them (via the finished event).

When a request is finished, the individual object is notified and it calculates the difference between start and finishing times which are then compared to a previously registered "Expected time" so as to know the difference.

Usually differences vary between 0 to 20 milliseconds.

The issue is when differences are showed as negative - meaning the task finished before the time it was expected to have passed.

The scheduler runs on a separate Thread.

Does anything seem to be wrong with the code?

Note: originally the requests were stored in an ArrayList and the delays would go up to 30ms sometimes, but nothing bad at all. As an attempt to bring that value down i replaced the ArrayList with a ConcurrentHashMap (to avoid exceptions while iterating), which does bring down the delay though also causes the stated issue.

What I have tried:

The logic behind the scheduler is easy. Im using a Delta Time mechanic, decrementing each request (in milliseconds) by each "frame" 's deltaTime and unregistering the request after the respective time (or more) has passed.

I won't be posting all of the code to avoid overcluttering the question unless it's needed.

The Scheduler's code

Java
public void registerListener(SchedulerListener listener, long seconds, long milliseconds)
{
    if(!isRunning)
        return;

    listener.onSchedulerStart();
    listeners.put(listener, secondsToMilliseconds(seconds) + milliseconds);
}

public void unregisterListener(SchedulerListener listener)
{
    if(!isRunning)
        return;

    listeners.remove(listener);

public void run()
{
    isRunning = true;

    long lastTime, currentTime, deltaTime;
    lastTime = System.currentTimeMillis();

    while(!Thread.currentThread().isInterrupted())
    {

        currentTime = System.currentTimeMillis();

        deltaTime = currentTime - lastTime;

        checkRequests(deltaTime);

        lastTime = currentTime;
    }
}

private void checkRequests(long deltaTime)
{
    long value;

    for(SchedulerListener listener : listeners.keySet())
    {
        value = listeners.get(listener) - deltaTime;

        listeners.put(listener, value);

        if(value <= 0)
        {
            listener.onSchedulerDone();
            listeners.remove(listener);
        }
    }
}
Posted
Comments
Richard MacCutchan 13-Dec-16 4:51am    
Does anything seem to be wrong with the code?
With respect to what? Do you understand that this will not be a real time application, so the delta times will always be arbitrary, depending on whatever else is running in the system.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900