Peter McCormick 6 년 전
부모
커밋
71a7a052ed

+ 53
- 0
src/Test/Java/zipcoder/SafeCopierTest.java 파일 보기

@@ -1,4 +1,57 @@
1 1
 package zipcoder;
2 2
 
3
+import io.zipcoder.SafeCopier;
4
+import org.junit.Assert;
5
+import org.junit.Before;
6
+import org.junit.Test;
7
+
3 8
 public class SafeCopierTest {
9
+    String intro;
10
+        @Before
11
+        public void setUp() {
12
+            intro = "It was the best of times,\n" +
13
+                    "it was the blurst of times,\n" +
14
+                    "it was the age of wisdom,\n" +
15
+                    "it was the age of foolishness,\n" +
16
+                    "it was the epoch of belief,\n" +
17
+                    "it was the epoch of incredulity,\n" +
18
+                    "it was the season of Light,\n" +
19
+                    "it was the season of Darkness,\n" +
20
+                    "it was the spring of hope,\n" +
21
+                    "it was the winter of despair,\n" +
22
+                    "we had everything before us,\n" +
23
+                    "we had nothing before us,\n" +
24
+                    "we were all going direct to Heaven,\n" +
25
+                    "we were all going direct the other way--\n" +
26
+                    "in short, the period was so far like the present period, that some of\n" +
27
+                    "its noisiest authorities insisted on its being received, for good or for\n" +
28
+                    "evil, in the superlative degree of comparison only. ";
29
+    }
30
+
31
+    @Test
32
+    public void safeTest() {
33
+        SafeCopier safeCopierTest = new SafeCopier(intro);
34
+
35
+        Thread monkey = new Thread(safeCopierTest);
36
+        Thread monkey1 = new Thread(safeCopierTest);
37
+        Thread monkey2 = new Thread(safeCopierTest);
38
+        Thread monkey3 = new Thread(safeCopierTest);
39
+        Thread monkey4 = new Thread(safeCopierTest);
40
+
41
+        monkey.start();
42
+        monkey1.start();
43
+        monkey2.start();
44
+        monkey3.start();
45
+        monkey4.start();
46
+
47
+        try {
48
+            Thread.sleep(1000);
49
+        } catch(InterruptedException e) {
50
+            System.out.println("MAIN INTERRUPTED");
51
+        }
52
+
53
+        String testString = safeCopierTest.copied;
54
+
55
+        Assert.assertEquals(intro, testString);
56
+    }
4 57
 }

+ 41
- 11
src/main/java/io/zipcoder/MonkeyTypewriter.java 파일 보기

@@ -1,7 +1,9 @@
1 1
 package io.zipcoder;
2 2
 
3 3
 public class MonkeyTypewriter {
4
+
4 5
     public static void main(String[] args) {
6
+
5 7
         String introduction = "It was the best of times,\n" +
6 8
                 "it was the blurst of times,\n" +
7 9
                 "it was the age of wisdom,\n" +
@@ -25,18 +27,40 @@ public class MonkeyTypewriter {
25 27
         // A Tale Of Two Cities.
26 28
 
27 29
         UnsafeCopier unsafeCopier = new UnsafeCopier(introduction);
30
+        SafeCopier safeCopier = new SafeCopier(introduction);
31
+
32
+        Thread[] unsafeMonkey = new Thread[5];
33
+        Thread[] safeMonkey = new Thread[5];
34
+
35
+        for (int i = 0; i < unsafeMonkey.length; i++) {
36
+            unsafeMonkey[i] = new Thread(unsafeCopier);
37
+            safeMonkey[i] = new Thread(safeCopier);
38
+        }
39
+
40
+        for (Thread thread : unsafeMonkey) {
41
+                thread.start();
42
+            }
43
+            for (Thread thread : safeMonkey) {
44
+                thread.start();
45
+            }
28 46
 
29
-        Thread monkey = new Thread(unsafeCopier);
30
-        Thread monkey1 = new Thread(unsafeCopier);
31
-        Thread monkey2 = new Thread(unsafeCopier);
32
-        Thread monkey3 = new Thread(unsafeCopier);
33
-        Thread monkey4 = new Thread(unsafeCopier);
47
+//       while ( unsafeCopier.stringIterator.hasNext() || safeCopier.stringIterator.hasNext()) {
48
+//
49
+//            for (int i = 0; i < unsafeMonkey.length; i++) {
50
+//                unsafeMonkey[i] = new Thread(unsafeCopier);
51
+//                safeMonkey[i] = new Thread(safeCopier);
52
+//            }
53
+//
54
+//            for (Thread thread : unsafeMonkey) {
55
+//                thread.start();
56
+//            }
57
+//            for (Thread thread : safeMonkey) {
58
+//                thread.start();
59
+//            }
60
+//
61
+//
62
+//        }
34 63
 
35
-        monkey.start();
36
-        monkey1.start();
37
-        monkey2.start();
38
-        monkey3.start();
39
-        monkey4.start();
40 64
 
41 65
         // This wait is here because main is still a thread and we want the main method to print the finished copies
42 66
         // after enough time has passed.
@@ -45,9 +69,15 @@ public class MonkeyTypewriter {
45 69
         } catch(InterruptedException e) {
46 70
             System.out.println("MAIN INTERRUPTED");
47 71
         }
48
-
72
+        System.out.println("_________________________UNSAFE_TEST__________________________________");
49 73
         System.out.println(unsafeCopier.copied);
50 74
 
75
+        System.out.println("_________________________SAFE_TEST____________________________________");
76
+        System.out.println(safeCopier.copied);
77
+
78
+
79
+
80
+
51 81
         // Print out the copied versions here.
52 82
     }
53 83
 }

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

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

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

@@ -10,7 +10,7 @@ public class UnsafeCopier extends Copier {
10 10
         super(toCopy);
11 11
 
12 12
     }
13
-    @Override
13
+
14 14
     public void run() {
15 15
 
16 16
         while(stringIterator.hasNext()) {