Created
March 2, 2023 11:43
-
-
Save ashraf267/176fc111265f067547bd550f63352c34 to your computer and use it in GitHub Desktop.
This is working but results are not correct - needs tweaking!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void main() { | |
// function call | |
fcfs([ | |
// then this, secondly | |
Task( | |
't1', 10, 20, | |
), | |
// by rule, this should exec. first | |
Task( | |
't2', 0, 25, | |
), | |
// then this, lastly | |
Task( | |
't3', 15, 30, | |
), | |
]); | |
// TODO: add total time taken | |
} | |
class Task { | |
String taskName; | |
int arrivalTime; | |
int exeTime; | |
Task(this.taskName, this.arrivalTime, this.exeTime); | |
// doTask function | |
void doTask(int taskId) { | |
// TODO: find ways to automate this potentially long if-else statements | |
if (taskId == 10) { | |
print("task 1 started!"); | |
} else if(taskId == 0) { | |
print("task 2 started!"); | |
} else { | |
print("task 3 started!"); | |
} | |
} | |
} | |
void fcfs(List<Task> tasks) async { | |
// stopwatch | |
final stopwatch = Stopwatch(); | |
// sort | |
tasks.sort((a, b) => a.arrivalTime.compareTo(b.arrivalTime)); | |
// todo | |
for(Task myTask in tasks) { | |
// print(myTask.arrivalTime); | |
print(myTask.taskName); | |
// call myTask.doTask and pass arg | |
int i = myTask.arrivalTime; | |
// add next task to queue | |
stopwatch.start(); | |
await Future.delayed(Duration(seconds: 2), () { | |
// todo | |
print("next task added to queue!"); | |
stopwatch.stop(); | |
print("- ${stopwatch.elapsed}"); | |
}); | |
print("wait time: ${myTask.exeTime - i}s"); | |
myTask.doTask(i); | |
print("completed in ${myTask.exeTime}s"); | |
} | |
print("\n\nall tasks completed!"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment