-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathTaskScheduler.java
More file actions
88 lines (77 loc) · 2.89 KB
/
TaskScheduler.java
File metadata and controls
88 lines (77 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package sbu.cs;
import com.sun.source.tree.TryTree;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class TaskScheduler
{
public static class Task implements Runnable
{
/*
------------------------- You don't need to modify this part of the code -------------------------
*/
String taskName;
int processingTime;
public Task(String taskName, int processingTime) {
this.taskName = taskName;
this.processingTime = processingTime;
}
public int getProcessingTime() {
return processingTime;
}
/*
------------------------- You don't need to modify this part of the code -------------------------
*/
@Override
public void run() {
/*
TODO
Simulate utilizing CPU by sleeping the thread for the specified processingTime
*/
try{
System.out.println(taskName + " is started ");
Thread.sleep(processingTime);
}catch(Exception e) {
System.out.println(e.getMessage());
}
}
}
public static ArrayList<String> doTasks(ArrayList<Task> tasks) throws InterruptedException {
ArrayList<String> finishedTasks = new ArrayList<>();
Collections.sort(tasks , Comparator.comparing(Task::getProcessingTime)); // for sorting the objects base on their processing time
/*
TODO
Create a thread for each given task, And then start them based on which task has the highest priority
(highest priority belongs to the tasks that take more time to be completed).
You have to wait for each task to get done and then start the next task.
Don't forget to add each task's name to the finishedTasks after it's completely finished.
*/
Thread thread;
for (int i = 0 ; i < tasks.size() ; i++) {
thread = new Thread(tasks.get(i));
thread.start();
try {
thread.join();
}catch (Exception e){
e.printStackTrace();
}
}
for (int i = tasks.size()-1 ; i >= 0 ; i--) { // coping the tasks array list in the finishedTasks array list
finishedTasks.add(tasks.get(i).taskName);
}
return finishedTasks;
}
public static void main(String[] args) throws InterruptedException {
// Test your code here
ArrayList<Task> tasks = new ArrayList<>();
tasks.add(new Task("A", 1000));
tasks.add(new Task("B", 1200));
tasks.add(new Task("C", 5000));
tasks.add(new Task("E", 2000));
tasks.add(new Task("F", 3000));
// for (int i = 0 ; i < tasks.size() ; i++ ) {
System.out.println(doTasks(tasks));
// }
}
}