|
@@ -2,17 +2,18 @@ import java.util.concurrent.ThreadLocalRandom;
|
|
|
|
|
|
public abstract class Mutex {
|
|
|
|
|
|
+ static int maxIter = 10;
|
|
|
+ static int minIter = 1;
|
|
|
+ static int msWait = 400;
|
|
|
+
|
|
|
public static void criticalSection(String mark) {
|
|
|
try {
|
|
|
- int maxIter = 10;
|
|
|
- int minIter = 1;
|
|
|
-
|
|
|
int rdIter = ThreadLocalRandom.current().nextInt(minIter, maxIter + 1);
|
|
|
|
|
|
System.out.println("^");
|
|
|
for (int k = 0; k < maxIter; k++) {
|
|
|
System.out.println(mark);
|
|
|
- Thread.sleep(500);
|
|
|
+ Thread.sleep(msWait);
|
|
|
}
|
|
|
System.out.println("v");
|
|
|
}
|
|
@@ -23,7 +24,8 @@ public abstract class Mutex {
|
|
|
|
|
|
public static void nonCriticalSection() {
|
|
|
try {
|
|
|
- Thread.sleep((int) (Math.random() * 3000));
|
|
|
+ int rdIter = ThreadLocalRandom.current().nextInt(minIter, maxIter + 1);
|
|
|
+ Thread.sleep(rdIter * msWait);
|
|
|
}
|
|
|
catch (InterruptedException e) {
|
|
|
// Nothing
|