+/EE360P

distributed systems

git clone https://git.julia.blue/+/EE360P

jspspikeFinished 243d7a64

master
1.2 KiB49 linesraw
1import java.util.Random;
2import java.lang.Thread;
3
4public class testFairUnfairBathroom implements Runnable {
5	final static int SIZE = 10;
6	
7    final FairUnifanBathroom bathroom;
8    final boolean ut;
9	
10	public testFairUnfairBathroom(FairUnifanBathroom bathroom, boolean ut) {
11		this.bathroom = bathroom;
12        this.ut = ut;
13	}
14	
15	public void run() {
16        Random rng = new Random();
17        try {
18            if (ut) {
19                bathroom.enterBathroomUT();
20            } else {
21                bathroom.enterBathroomOU();
22            }
23
24            Thread.sleep(rng.nextInt(100));
25
26            if (ut) {
27                bathroom.leaveBathroomUT();
28            } else {
29                bathroom.leaveBathroomOU();
30            }
31        } catch (InterruptedException e) {
32            System.out.println(e);
33        }
34	}
35	
36	public static void main(String[] args) {
37		FairUnifanBathroom bathroom = new FairUnifanBathroom();
38		Thread[] t = new Thread[SIZE];
39        Random rng = new Random();
40		
41		for (int i = 0; i < SIZE; ++i) {
42			t[i] = new Thread(new testFairUnfairBathroom(bathroom, rng.nextBoolean()));
43		}
44		
45		for (int i = 0; i < SIZE; ++i) {
46			t[i].start();
47		}
48    }
49}