-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathController.java
More file actions
45 lines (35 loc) · 1.84 KB
/
Controller.java
File metadata and controls
45 lines (35 loc) · 1.84 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
package sbu.cs.Semaphore;
import java.util.concurrent.Semaphore;
public class Controller {
/**
* A number of operators are trying to access a resource in parallel to each other.
* A maximum of 2 operators should access the resource concurrently. Once 2 operators have entered
the critical section, others must wait for at least one of them to leave.
* Use a Semaphore to solve this synchronization problem. Semaphores behave similarly to locks,
with the exception that they can allow more than 1 thread to enter the critical section.
* Similar to a lock, a single instance of the Semaphore class must be shared between multiple
threads, otherwise the threads won't be synchronized.
* You are allowed to add new code to any of the three provided classes.
* Do NOT change any existing lines of code. You can add new attributes and method parameters.
* Note that every time a thread accesses the resource, you must print its Name and the System Time.
*/
public static void main(String[] args) {
Semaphore semaphore = new Semaphore(2);
Operator operator1 = new Operator("operator1", semaphore);
Operator operator2 = new Operator("operator2", semaphore);
Operator operator3 = new Operator("operator3", semaphore);
Operator operator4 = new Operator("operator4", semaphore);
Operator operator5 = new Operator("operator5", semaphore);
operator1.start();
operator2.start();
operator3.start();
operator4.start();
operator5.start();
/*
TODO
Use a Semaphore to solve the synchronization problem.
Every time a thread accesses the resource, print its Name and the current System Time.
Combine the 4 resulting blocks to create the final matrix product and return it.
*/
}
}