|
@@ -34,18 +34,42 @@ public class MonkeyTypewriter {
|
34
|
34
|
// Do all of the Monkey / Thread building here
|
35
|
35
|
// For each Copier(one safe and one unsafe), create and start 5 monkeys copying the introduction to
|
36
|
36
|
// A Tale Of Two Cities.
|
|
37
|
+ //Safe threads
|
|
38
|
+ SafeCopier sc = new SafeCopier(introduction); //contstructing safe/unsafe copies of the introduction
|
|
39
|
+ UnsafeCopier usc = new UnsafeCopier(introduction);
|
37
|
40
|
|
38
|
|
-
|
39
|
|
-
|
40
|
|
-
|
|
41
|
+ Thread monkeySafe1 = new Thread(sc);
|
|
42
|
+ Thread monkeySafe2 = new Thread(sc);
|
|
43
|
+ Thread monkeySafe3 = new Thread(sc);
|
|
44
|
+ Thread monkeySafe4 = new Thread(sc);
|
|
45
|
+ Thread monkeySafe5 = new Thread(sc);
|
|
46
|
+ //Unsafe threads
|
|
47
|
+ Thread monkeyUnsafe1 = new Thread(usc);
|
|
48
|
+ Thread monkeyUnsafe2 = new Thread(usc);
|
|
49
|
+ Thread monkeyUnsafe3 = new Thread(usc);
|
|
50
|
+ Thread monkeyUnsafe4 = new Thread(usc);
|
|
51
|
+ Thread monkeyUnsafe5 = new Thread(usc);
|
|
52
|
+ //For each one of these copiers, make 5 monkeys start copying the intro
|
|
53
|
+ monkeySafe1.start();
|
|
54
|
+ monkeySafe2.start();
|
|
55
|
+ monkeySafe3.start();
|
|
56
|
+ monkeySafe4.start();
|
|
57
|
+ monkeySafe5.start();
|
|
58
|
+ //start unsafe copies
|
|
59
|
+ monkeyUnsafe1.start();
|
|
60
|
+ monkeyUnsafe2.start();
|
|
61
|
+ monkeyUnsafe3.start();
|
|
62
|
+ monkeyUnsafe4.start();
|
|
63
|
+ monkeyUnsafe5.start();
|
41
|
64
|
// This wait is here because main is still a thread and we want the main method to print the finished copies
|
42
|
65
|
// after enough time has passed.
|
43
|
66
|
try {
|
44
|
|
- Thread.sleep(1000);
|
|
67
|
+ Thread.sleep(1000); //wait time between each thread request
|
45
|
68
|
} catch(InterruptedException e) {
|
46
|
69
|
System.out.println("MAIN INTERRUPTED");
|
47
|
70
|
}
|
48
|
|
-
|
49
|
71
|
// Print out the copied versions here.
|
|
72
|
+ System.out.println(sc.copied + "\n"); //printing will be exactly as expected since it is locked
|
|
73
|
+ System.out.println(usc.copied); //printing of this will have unpredictable behavior and results are not safe due to lack of lock
|
50
|
74
|
}
|
51
|
75
|
}
|