-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMultithreadedService(ORIGINAL).java
123 lines (89 loc) · 4.83 KB
/
MultithreadedService(ORIGINAL).java
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import java.util.Random;
/*
* File: MultithreadedService.java
* Course: 20HT - Operating Systems - 1DV512
* Author: *State your name and LNU student ID here! (e.g., ab222cd)*
* Date: December 2020
*/
// TODO: put this source code file into a new Java package with meaningful name (e.g., dv512.YourStudentID)!
// You can implement additional fields and methods in code below, but
// you are not allowed to rename or remove any of it!
// Additionally, please remember that you are not allowed to use any third-party libraries
public class MultithreadedService {
// TODO: implement a nested public class titled Task here
// which must have an integer ID and specified burst time (duration) in milliseconds,
// see below
// Add further fields and methods to it, if necessary
// As the task is being executed for the specified burst time,
// it is expected to simply go to sleep every X milliseconds (specified below)
// Random number generator that must be used for the simulation
Random rng;
// ... add further fields, methods, and even classes, if necessary
public MultithreadedService (long rngSeed) {
this.rng = new Random(rngSeed);
}
public void reset() {
// TODO - remove any information from the previous simulation, if necessary
}
// If the implementation requires your code to throw some exceptions,
// you are allowed to add those to the signature of this method
public void runNewSimulation(final long totalSimulationTimeMs,
final int numThreads, final int numTasks,
final long minBurstTimeMs, final long maxBurstTimeMs, final long sleepTimeMs) {
reset();
// TODO:
// 1. Run the simulation for the specified time, totalSimulationTimeMs
// 2. While the simulation is running, use a fixed thread pool with numThreads
// (see https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/concurrent/Executors.html#newFixedThreadPool(int) )
// to execute Tasks (implement the respective class, see above!)
// 3. The total maximum number of tasks is numTasks,
// and each task has a burst time (duration) selected randomly
// between minBurstTimeMs and maxBurstTimeMs (inclusive)
// 4. The implementation should assign sequential task IDs to the created tasks (0, 1, 2...)
// and it should assign them to threads in the same sequence (rather any other scheduling approach)
// 5. When the simulation time is up, it should make sure to stop all of the currently executing
// and waiting threads!
}
public void printResults() {
// TODO:
System.out.println("Completed tasks:");
// 1. For each *completed* task, print its ID, burst time (duration),
// its start time (moment since the start of the simulation), and finish time
System.out.println("Interrupted tasks:");
// 2. Afterwards, print the list of tasks IDs for the tasks which were currently
// executing when the simulation was finished/interrupted
System.out.println("Waiting tasks:")
// 3. Finally, print the list of tasks IDs for the tasks which were waiting for execution,
// but were never started as the simulation was finished/interrupted
}
// If the implementation requires your code to throw some exceptions,
// you are allowed to add those to the signature of this method
public static void main(String args[]) {
// TODO: replace the seed value below with your birth date, e.g., "20001001"
final long rngSeed = 00000000;
// Do not modify the code below — instead, complete the implementation
// of other methods!
MultithreadedService service = new MultithreadedService(rngSeed);
final int numSimulations = 3;
final long totalSimulationTimeMs = 15*1000L; // 15 seconds
final int numThreads = 4;
final int numTasks = 30;
final long minBurstTimeMs = 1*1000L; // 1 second
final long maxBurstTimeMs = 10*1000L; // 10 seconds
final long sleepTimeMs = 100L; // 100 ms
for (int i = 0; i < numSimulations; i++) {
System.out.println("Running simulation #" + i);
service.runNewSimulation(totalSimulationTimeMs,
numThreads, numTasks,
minBurstTimeMs, maxBurstTimeMs, sleepTimeMs);
System.out.println("Simulation results:"
+ "\n" + "----------------------");
service.printResults();
System.out.println("\n");
}
System.out.println("----------------------");
System.out.println("Exiting...");
// If your program has not completed after the message printed above,
// it means that some threads are not properly stopped! -> this issue will affect the grade
}
}