123456789101112131415161718192021222324252627282930313233343536373839404142 |
- import java.util.concurrent.ThreadLocalRandom;
- public abstract class Mutex {
- static int maxIter = 10;
- static int minIter = 1;
- static int msWait = 350;
- public static void criticalSection(String mark) {
- try {
- int rdIter = ThreadLocalRandom.current().nextInt(minIter, maxIter + 1);
-
- System.out.println("^");
- for (int k = 0; k < rdIter; k++) {
- System.out.println(mark);
- Thread.sleep(msWait);
- }
- System.out.println("v");
- }
- catch (InterruptedException e) {
- // Nothing
- }
- }
- public static void nonCriticalSection() {
- try {
- int rdIter = ThreadLocalRandom.current().nextInt(minIter, maxIter + 1);
- Thread.sleep(rdIter * msWait);
- }
- catch (InterruptedException e) {
- // Nothing
- }
- }
- // Take ownership
- public abstract void p(int id);
- // Release ownership
- public abstract void v(int id);
- public abstract void describe();
- }
|