#10 vincesima

오픈
vincesima vincesima/ZCW-MonkeysTypewritersOhMy:master 에서 master 로 1 commits 를 머지하려 합니다
3개의 변경된 파일54개의 추가작업 그리고 5개의 파일을 삭제
  1. 17
    3
      src/main/java/io/zipcoder/MonkeyTypewriter.java
  2. 30
    2
      src/main/java/io/zipcoder/SafeCopier.java
  3. 7
    0
      src/main/java/io/zipcoder/UnsafeCopier.java

+ 17
- 3
src/main/java/io/zipcoder/MonkeyTypewriter.java 파일 보기

23
         // Do all of the Monkey / Thread building here
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
24
         // For each Copier(one safe and one unsafe), create and start 5 monkeys copying the introduction to
25
         // A Tale Of Two Cities.
25
         // A Tale Of Two Cities.
26
+        UnsafeCopier unsafe = new UnsafeCopier(introduction);
27
+        new Thread(unsafe).start();
28
+        new Thread(unsafe).start();
29
+        new Thread(unsafe).start();
30
+        new Thread(unsafe).start();
31
+        new Thread(unsafe).start();
32
+
33
+        SafeCopier safe = new SafeCopier(introduction);
34
+        new Thread(safe).start();
35
+        new Thread(safe).start();
36
+        new Thread(safe).start();
37
+        new Thread(safe).start();
38
+        new Thread(safe).start();
39
+
26
 
40
 
27
 
41
 
28
         // This wait is here because main is still a thread and we want the main method to print the finished copies
42
         // This wait is here because main is still a thread and we want the main method to print the finished copies
29
         // after enough time has passed.
43
         // after enough time has passed.
30
         try {
44
         try {
31
             Thread.sleep(1000);
45
             Thread.sleep(1000);
32
-        } catch(InterruptedException e) {
46
+        } catch (InterruptedException e) {
33
             System.out.println("MAIN INTERRUPTED");
47
             System.out.println("MAIN INTERRUPTED");
34
         }
48
         }
35
-
36
-        // Print out the copied versions here.
49
+        System.out.println("Unsafe: " + unsafe.copied + "\n\n\n\n\n\n");
50
+        System.out.println("Safe: " + safe.copied);
37
     }
51
     }
38
 }
52
 }

+ 30
- 2
src/main/java/io/zipcoder/SafeCopier.java 파일 보기

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

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

1
 package io.zipcoder;
1
 package io.zipcoder;
2
 
2
 
3
+import java.util.Iterator;
4
+
3
 /**
5
 /**
4
  * Modify the run function so that the monkeys each grab the next word and write it to the copy.
6
  * Modify the run function so that the monkeys each grab the next word and write it to the copy.
5
  */
7
  */
10
     }
12
     }
11
 
13
 
12
     public void run() {
14
     public void run() {
15
+        while (stringIterator.hasNext()) {
16
+                        copied += stringIterator.next() + " ";
17
+                    }
18
+                copied = copied.trim();
19
+
13
     }
20
     }
14
 }
21
 }