-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathTaskScheduler.java
More file actions
65 lines (58 loc) · 1.79 KB
/
TaskScheduler.java
File metadata and controls
65 lines (58 loc) · 1.79 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
package sbu.cs;
import java.util.ArrayList;
import java.util.Collection;
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;
}
/*
------------------------- You don't need to modify this part of the code -------------------------
*/
@Override
public void run() {
try {
Thread.sleep(processingTime);
} catch (InterruptedException e) {
System.out.println("thread Interrupted.");
}
}
}
public static ArrayList<String> doTasks(ArrayList<Task> tasks)
{
ArrayList<String> finishedTasks = new ArrayList<>();
for (int i = 1; i < tasks.size(); i++) {
int j = i;
while (j > 0 && tasks.get(j).processingTime > tasks.get(j - 1).processingTime) {
Task save = tasks.get(j);
tasks.set(j, tasks.get(j - 1));
tasks.set(j - 1, save);
j--;
}
}
for (Task t: tasks) {
finishedTasks.add(t.taskName);
}
for (Task i: tasks) {
Thread thread = new Thread(i);
thread.start();
try {
thread.join();
} catch (InterruptedException e) {
System.out.println("Interrupted.");
}
}
return finishedTasks;
}
public static void main(String[] args) {
}
}