nafis nibir 6 년 전
부모
커밋
78b3e7f718

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

@@ -9,6 +9,7 @@ import java.util.Iterator;
9 9
 public abstract class Copier implements Runnable {
10 10
     // We use an iterator so each monkey / thread can copy an individual word.
11 11
     public Iterator<String> stringIterator;
12
+
12 13
     public String copied;
13 14
 
14 15
     public Copier(String toCopy) {

+ 30
- 0
src/main/java/io/zipcoder/MonkeyTypewriter.java 파일 보기

@@ -20,9 +20,35 @@ public class MonkeyTypewriter {
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 22
 
23
+        StringBuilder sb = new StringBuilder();
24
+        for(int i = 0; i < 500; i++){
25
+            sb.append(introduction + " ");
26
+        }
27
+        String big = sb.toString();
23 28
         // Do all of the Monkey / Thread building here
24 29
         // For each Copier(one safe and one unsafe), create and start 5 monkeys copying the introduction to
25 30
         // A Tale Of Two Cities.
31
+        UnsafeCopier unsafe = new UnsafeCopier(introduction);
32
+        SafeCopier safe = new SafeCopier(introduction);
33
+
34
+        SafeCopier bigSafe = new SafeCopier(big);
35
+
36
+
37
+        for(int i = 0; i < 5; i++){
38
+            new Thread(unsafe).start();
39
+        }
40
+
41
+        for(int i = 0; i < 5; i++){
42
+            new Thread(safe).start();
43
+        }
44
+
45
+//        for(int i = 0; i < 5; i++){
46
+//            new Thread(bigSafe).start();
47
+//        }
48
+
49
+
50
+
51
+
26 52
 
27 53
 
28 54
         // This wait is here because main is still a thread and we want the main method to print the finished copies
@@ -33,6 +59,10 @@ public class MonkeyTypewriter {
33 59
             System.out.println("MAIN INTERRUPTED");
34 60
         }
35 61
 
62
+        System.out.println(unsafe.copied);
63
+        System.out.println("============");
64
+        System.out.println(safe.copied);
65
+        //System.out.println(bigSafe.copied);
36 66
         // Print out the copied versions here.
37 67
     }
38 68
 }

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

@@ -1,8 +1,31 @@
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
+
12
+    Lock lock = new ReentrantLock();
13
+
14
+    public SafeCopier(String toCopy){
15
+        super(toCopy);
16
+    }
17
+
18
+    public void run(){
19
+
20
+        while(stringIterator.hasNext()){
21
+            lock.lock();
22
+            //System.out.println("curr thread :" + Thread.currentThread().getName());
23
+            copied += stringIterator.next() + " ";
24
+            lock.unlock();
25
+        }
26
+        //System.out.println("curr thread :" + Thread.currentThread().getName());
27
+
28
+    }
29
+
30
+
8 31
 }

+ 6
- 0
src/main/java/io/zipcoder/UnsafeCopier.java 파일 보기

@@ -10,5 +10,11 @@ public class UnsafeCopier extends Copier {
10 10
     }
11 11
 
12 12
     public void run() {
13
+        while (stringIterator.hasNext()) {
14
+            //System.out.println(Thread.currentThread().getName());
15
+            copied += stringIterator.next() + " ";
16
+            //copied += " ";
17
+        }
18
+        //copied += stringIterator.next() + " ";
13 19
     }
14 20
 }