소스 검색

Merge c5f539f73081d5823cc3a034ac96a0c730bdff8d into dbc7033eb3d6c33c19985384bea0b1a977cc30d3

Lawrence Wu 6 년 전
부모
커밋
a37c0b1421
No account linked to committer's email

+ 1
- 0
src/main/java/io/zipcoder/Copier.java 파일 보기

@@ -18,4 +18,5 @@ public abstract class Copier implements Runnable {
18 18
     }
19 19
 
20 20
     public abstract void run();
21
+
21 22
 }

+ 25
- 7
src/main/java/io/zipcoder/MonkeyTypewriter.java 파일 보기

@@ -1,7 +1,7 @@
1 1
 package io.zipcoder;
2 2
 
3 3
 public class MonkeyTypewriter {
4
-    public static void main(String[] args) {
4
+    public static void main(String[] args) throws InterruptedException {
5 5
         String introduction = "It was the best of times,\n" +
6 6
                 "it was the blurst of times,\n" +
7 7
                 "it was the age of wisdom,\n" +
@@ -19,11 +19,27 @@ public class MonkeyTypewriter {
19 19
                 "in short, the period was so far like the present period, that some of\n" +
20 20
                 "its noisiest authorities insisted on its being received, for good or for\n" +
21 21
                 "evil, in the superlative degree of comparison only.";
22
-
23
-        // Do all of the Monkey / Thread building here
24
-        // For each Copier(one safe and one unsafe), create and start 5 monkeys copying the introduction to
25
-        // A Tale Of Two Cities.
26
-
22
+        //unsafe threads
23
+        UnsafeCopier unsafeCopier = new UnsafeCopier(introduction);
24
+        Thread[] thread = new Thread[5];
25
+        for (int i = 0; i < thread.length; i++){
26
+            thread[i] = new Thread(unsafeCopier);
27
+            thread[i].start();
28
+        }
29
+        //make sure the background thread is run after the threads do their jobs first
30
+//        for (Thread threads : thread){
31
+//            threads.join();
32
+//        }
33
+        //safe
34
+        SafeCopier safeCopier = new SafeCopier(introduction);
35
+        Thread[] safeThread = new Thread[5];
36
+        for (int i = 0; i < safeThread.length; i++){
37
+            safeThread[i] = new Thread(safeCopier);
38
+            safeThread[i].start();
39
+        }
40
+//        for (Thread safetyThreads : safeThread){
41
+//            safetyThreads.join();
42
+//        }
27 43
 
28 44
         // This wait is here because main is still a thread and we want the main method to print the finished copies
29 45
         // after enough time has passed.
@@ -32,7 +48,9 @@ public class MonkeyTypewriter {
32 48
         } catch(InterruptedException e) {
33 49
             System.out.println("MAIN INTERRUPTED");
34 50
         }
35
-
36 51
         // Print out the copied versions here.
52
+        System.out.println("Unsafe Monkeys: " + "\n" + unsafeCopier.copied);
53
+        System.out.println("----------------------------------------------");
54
+        System.out.println("Safe Monkeys: " + "\n" + safeCopier.copied);
37 55
     }
38 56
 }

+ 18
- 1
src/main/java/io/zipcoder/SafeCopier.java 파일 보기

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

+ 4
- 1
src/main/java/io/zipcoder/UnsafeCopier.java 파일 보기

@@ -3,12 +3,15 @@ package io.zipcoder;
3 3
 /**
4 4
  * Modify the run function so that the monkeys each grab the next word and write it to the copy.
5 5
  */
6
-public class UnsafeCopier extends Copier {
6
+public class UnsafeCopier extends Copier implements Runnable {
7 7
 
8 8
     public UnsafeCopier(String toCopy) {
9 9
         super(toCopy);
10 10
     }
11 11
 
12 12
     public void run() {
13
+        while (stringIterator.hasNext()){
14
+            copied += stringIterator.next() + " ";
15
+        }
13 16
     }
14 17
 }