Selaa lähdekoodia

Display ongoing critical section

DricomDragon 5 vuotta sitten
vanhempi
commit
b53d82063a
3 muutettua tiedostoa jossa 15 lisäystä ja 4 poistoa
  1. 13 2
      ta1/Mutex.java
  2. 1 1
      ta1/Task.java
  3. 1 1
      ta1/makefile

+ 13 - 2
ta1/Mutex.java

@@ -1,9 +1,20 @@
+import java.util.concurrent.ThreadLocalRandom;
 
 public abstract class Mutex {
 
-	public static void criticalSection() {
+	public static void criticalSection(char mark) {
 		try {
-			Thread.sleep((int) (Math.random() * 3000));
+			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);
+			}
+			System.out.println("v");
 		}
 		catch (InterruptedException e) {
 			// Nothing

+ 1 - 1
ta1/Task.java

@@ -20,7 +20,7 @@ class Task extends Thread {
 			critical.p(id);
 
 			System.out.println( name + " is CRITICAL!");
-			Mutex.criticalSection();
+			Mutex.criticalSection('|');
 
 			critical.v(id);
 

+ 1 - 1
ta1/makefile

@@ -1,4 +1,4 @@
-Main.class : Main.java
+Main.class : *.java
 	javac Main.java
 
 run : Main.class