2

I am working on a simulation program in java, and we are finding that we need a lightweight java process to kick itself off at a certain (randomly generated) time, perform a few tasks, and then schedule the next task (randomly generated as well as generated from the results of the first task).

The program currently runs in a linux environment, but I would like to keep options open such that it could be run on OSX and Windows environments in the future.

How would I schedule these in java without using too much resources on the scheduling?

2
  • 1
    You can't have a process start off randomly unless it's made to be spawned randomly from another process. One Java process that at random intervals executes another Java process. Commented Aug 29, 2013 at 23:48
  • I guess I should clarify. That point. The first task is manually started, all tasks after that are randomly generated and based off of the results of the previous task. Commented Aug 29, 2013 at 23:51

3 Answers 3

3

As recommended in ScheduledThreadPoolExecutor javadocs use it in favor of Timer

A ThreadPoolExecutor that can additionally schedule commands to run after a given delay, or to execute periodically. This class is preferable to Timer when multiple worker threads are needed, or when the additional flexibility or capabilities of ThreadPoolExecutor (which this class extends) are required.

public class TaskTimer {
    public static void main(String[] args) {
        ScheduledExecutorService svc = Executors.newScheduledThreadPool(0);
        svc.schedule(new Task(svc), 2, TimeUnit.SECONDS);
    }
}

class Task implements Callable<Result> {

    private ScheduledExecutorService svc;

    public Task(ScheduledExecutorService svc) {
        this.svc = svc;
    }

    @Override
    public Result call() throws Exception {
        Result result = compute();
        Task t = nextTask(result);
        svc.schedule(t, 2, TimeUnit.SECONDS);
        return result;
    }

    private Task nextTask(Result result) {
        return new Task(svc);
    }

    private Result compute() {
        System.out.println("Computing Result");
        return new Result();
    }

}

class Result {

}
Sign up to request clarification or add additional context in comments.

Comments

1

For maximum portability let the java program run continuously and just do things at certain times using the standard java Timer

If you want persistent timers, that are resistant to crashes and restarts, and if you don't mind using a Java EE server such as Glassfish (I would recommend it even for a lightweight task), then you can use the persistent timers that are standard in Java EE.

3 Comments

I think its fair to say that if such a continous program was written right it would use next to no resources. (And if it isnt written right it can still go on the dailyWTF
Correct. Next to zero resources. It is extremely efficient. Additionally, the Timer has an interface to allow cancelling a previously scheduled task, also efficiently.
as a subsequent answer mentions, ScheduledThreadPoolExecutor is a better choice if your tasks are doing something non-trivial. Timer will cause "bunching" if your tasks take some time to complete that is significant relative to the resolution of the schedule.
0

You can use a priority queue

essentially you put first task in to the priority queue

pop it

that in turns generates random tasks assign priority to each of those tasks

the ordering will happen naturally when you add them to the priority queue.

start popping based on how much time you need to wait if a particular tasks requires a particular time to wait.

then do something like this.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.