Преглед изворни кода

Merge 6522e5a6ffdbcb590ca175d06ec69106924d51c7 into dbc7033eb3d6c33c19985384bea0b1a977cc30d3

carolynnmarie пре 6 година
родитељ
комит
f9bd4ad147
No account linked to committer's email

+ 35
- 4
src/main/java/io/zipcoder/MonkeyTypewriter.java Прегледај датотеку

@@ -1,9 +1,11 @@
1 1
 package io.zipcoder;
2 2
 
3
-public class MonkeyTypewriter {
4
-    public static void main(String[] args) {
3
+
4
+public class MonkeyTypewriter{
5
+
6
+    public static void main(String[] args) throws InterruptedException {
5 7
         String introduction = "It was the best of times,\n" +
6
-                "it was the blurst of times,\n" +
8
+                "it was the worst of times,\n" +
7 9
                 "it was the age of wisdom,\n" +
8 10
                 "it was the age of foolishness,\n" +
9 11
                 "it was the epoch of belief,\n" +
@@ -20,9 +22,32 @@ public class MonkeyTypewriter {
20 22
                 "its noisiest authorities insisted on its being received, for good or for\n" +
21 23
                 "evil, in the superlative degree of comparison only.";
22 24
 
25
+
26
+
23 27
         // Do all of the Monkey / Thread building here
24 28
         // For each Copier(one safe and one unsafe), create and start 5 monkeys copying the introduction to
25
-        // A Tale Of Two Cities.
29
+        // A Tale Of Two Cities by Charles Dickens.
30
+
31
+        UnsafeCopier notSafe = new UnsafeCopier(introduction);
32
+        Thread[] notSafeThreads = new Thread[5];
33
+        for(int i=0; i<notSafeThreads.length; i++){
34
+            notSafeThreads[i] = new Thread(notSafe);
35
+            notSafeThreads[i].start();
36
+        }
37
+        for(Thread threads: notSafeThreads){
38
+            threads.join();
39
+        }
40
+
41
+
42
+        SafeCopier safe = new SafeCopier(introduction);
43
+        Thread[] safeThread = new Thread[5];
44
+        for(int i = 0; i<safeThread.length; i++){
45
+            safeThread[i] = new Thread(safe);
46
+            safeThread[i].start();
47
+        }
48
+        for(Thread threads: safeThread){
49
+            threads.join();
50
+       }
26 51
 
27 52
 
28 53
         // This wait is here because main is still a thread and we want the main method to print the finished copies
@@ -34,5 +59,11 @@ public class MonkeyTypewriter {
34 59
         }
35 60
 
36 61
         // Print out the copied versions here.
62
+        System.out.println(notSafe.copied);
63
+        System.out.println("____________________________________________");
64
+        System.out.println(safe.copied);
65
+
66
+
37 67
     }
68
+
38 69
 }

+ 22
- 1
src/main/java/io/zipcoder/SafeCopier.java Прегледај датотеку

@@ -1,8 +1,29 @@
1 1
 package io.zipcoder;
2 2
 
3
+import java.util.concurrent.locks.Lock;
4
+import java.util.concurrent.locks.ReentrantLock;
5
+
6
+
3 7
 /**
4 8
  * Make this extend the Copier like `UnsafeCopier`, except use locks to make sure that the actual intro gets printed
5 9
  * correctly every time.  Make the run method thread safe.
6 10
  */
7
-public class SafeCopier {
11
+public class SafeCopier extends Copier {
12
+
13
+    Lock shared = new ReentrantLock() {
14
+    };
15
+
16
+    public SafeCopier(String toCopy) {
17
+        super(toCopy);
18
+    }
19
+
20
+    public void run() {
21
+        shared.lock();
22
+        while(stringIterator.hasNext()){
23
+            copied += stringIterator.next() + " ";
24
+        }
25
+        shared.unlock();
26
+
27
+
28
+    }
8 29
 }

+ 11
- 0
src/main/java/io/zipcoder/UnsafeCopier.java Прегледај датотеку

@@ -9,6 +9,17 @@ public class UnsafeCopier extends Copier {
9 9
         super(toCopy);
10 10
     }
11 11
 
12
+
12 13
     public void run() {
14
+        try {
15
+            Thread.sleep(30);
16
+        } catch(InterruptedException e) {
17
+            System.out.println("MAIN INTERRUPTED");
18
+        }
19
+        while(stringIterator.hasNext()){
20
+           copied += stringIterator.next() + " ";
21
+        }
13 22
     }
23
+
14 24
 }
25
+