Question

I am writing J2SE app which purpose is to every day sent certain amount of request towards remote server. There is limited amount of request to be sent per day, even per hour - 100K req per day && 10K per hour => aprox. 1 req per 2.8 sec.

I wrote just sample program which prints something every 15sec - as repeating I used ScheduledExecutorService & ScheduledFuture where I specified my runnable, no start delay, and repeat delay=15000, time unit=MILLISECONDS.

In my testing runnable I tried to simmulate to do something in loop (just simple println() and with sleep(2800) between these iterations) - because I am in scheduled task with 15sec between execution next, I just realised I need 6 times to print => 6*2800=14000 which is fine upto 15000. But as I see when I sleep it, next execution of whole task which supposed to happen after 15000ms happens actually after: 14000ms + 15000ms. I guess this sleep(2800) has some effect to it.

As I said, I need to repeat certain requests sending once a day, with some time delay between sending - but to don't touch overal execution of this task after next 24h. how can I achieve this?

using: ScheduledFuture , ScheduledExecutorService , ThreadFactory

if anyone can help here is my code https://db.tt/vznt4PWG ,its an Eclipse project.

Was it helpful?

Solution

Solution:

ScheduledExecutorService scheduledExecutor = Executors.newSingleThreadScheduledExecutor(new     ThreadFactory() {

        @Override
        public Thread newThread(Runnable runnable) {
            return new Thread(runnable, "MY_REPEATING_JOB");
        }
});
Runnable jobToExecute = new MyJob();
scheduledExecutor.scheduleWithFixedDelay(jobToExecute, 0, 1, TimeUnit.NANOSECONDS);

I am controlling wait inside MyJob in run() method of Runnable - basicaly when some operation ends I count difference time up to I want to wait, and I do sleep() with this time.

Task is executing repeatedly, until I do somewhere in my code shutdown() of executor.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top