0% found this document useful (0 votes)
35 views4 pages

In-Memory Task Scheduler Design

The document outlines the requirements and design for an in-memory task scheduler, focusing on features like scheduled vs immediate execution, task priority, cancellation, execution time limits, and parallel execution. It includes interfaces for tasks, task queues, and a task scheduler implementation, emphasizing the need for concurrency handling in a multithreaded environment. Additionally, it discusses task chaining and the management of task priorities during execution.

Uploaded by

Shubham Aggarwal
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views4 pages

In-Memory Task Scheduler Design

The document outlines the requirements and design for an in-memory task scheduler, focusing on features like scheduled vs immediate execution, task priority, cancellation, execution time limits, and parallel execution. It includes interfaces for tasks, task queues, and a task scheduler implementation, emphasizing the need for concurrency handling in a multithreaded environment. Additionally, it discusses task chaining and the management of task priorities during execution.

Uploaded by

Shubham Aggarwal
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

In-Memory Task Scheduler

Requirements:
1. Scheduled Time vs Immediate
2. Priority (1 - 10: 1 is highest, 10 is lowest)
a. What happens a low priority task is executed and a high priority task is
enqueued
3. One time vs periodic
4. Cancel tasks
a. What happens when cancelling tasks already getting executed.
5. Execution time limit
a. What happens when the task crosses the execution time limit.
6. Parallel Execution (Multiple tasks executing in parallel)
a. How many tasks gets executed in parallel
7. Task chaining?
a. One task is done, then another task happens.
i. How to manage task priorities?

Non-Functional:
1. Fast enough in identifying the tasks to be scheduled correctly.
2. Multithreaded environment. So, handle concurrency. That is one task should be
executed once only (unless periodic).

Example: [Link]
kiss-interview-series-753107c0104c
[Link]

// TaskScheduler
// What all tasks are there in the list?
// → Task Queue to keep the list of tasks in some sorted
manner
// What task can be executed now?
// Execute the tasks that can be executed now.

// TaskQueue -> keeps the list of tasks in some sorted manner.


// → OneTimeTaskQueue, ScheduledTaskQueue
// TaskExecutor
// -> Contains a threadpool for execution.
// -> Executes a task
// -> hasCapacity():
capacity is always equivalent to the number of threads. If an additional task is
enqueued but can’t take it, it will throw an exception.

// Task
interface Task {
String getUniqueId(); // unique name for the task.
int getPriority();
long executeAt(); // returns time when the task should
be executed in seconds.
void execute();
}

interface PeriodicTask extends Task {


Duration period(); // returns periodic duration.
}

interface TaskQueue {
Optional<Task> poll(); // returns the task that can be
executed.
void add(Task task, bool replace); // adds a new task
void remove(Task task); // removes existing task or
throws exception if no task exist
}
interface TaskExecutor {
bool canExecute();
void execute(Task task);
}

abstract class TaskEnqueueRequest {


String uniqueId;
Runnable runnable;
long delay = 0;
int priority;
}

class OneTimeTaskEnqueueRequest extends TaskEnqueueRequest


{
}

class PeriodicTaskEnqueueRequest extends


TaskEnqueueRequest {
}

interface TaskScheduler {
void enqueue(OneTimeTaskEnqeueRequest request);
void enqueue(PeriodicTaskEnqeueRequest request);
void cancel(String uniqueId);
}

class TaskSchedulerImpl extends TaskScheduler {

class TasksManager {
private static TasksManager sharedInstance;
private ScheduledExecutorService scheduler;
private TasksManager() {
}

static TasksManager instance() {


if (sharedInstance != null) {
return sharedInstance;
}
synchronized([Link]) {
if (sharedInstance == null) {
sharedInstance = TasksManager();
}
}
return sharedInstance;
};

void enqueue(Task task) {}


void cancel(String uniqueId) {};

private void executePendingTasks() {

private List<Task> getTasksToBeExecuted() {


}
}

You might also like