소스 검색

Merge a8c990ab7973c062f3b54ee48ecfc28e5631567b into dbc7033eb3d6c33c19985384bea0b1a977cc30d3

gjarant 6 년 전
부모
커밋
db9abfe7c7
No account linked to committer's email

+ 47
- 0
src/Test/java/io/zipcoder/SafeCopierTest.java 파일 보기

@@ -0,0 +1,47 @@
1
+package io.zipcoder;
2
+
3
+import org.junit.Before;
4
+import org.junit.Test;
5
+
6
+import static org.junit.Assert.*;
7
+
8
+public class SafeCopierTest {
9
+
10
+    String introduction;
11
+
12
+    @Before
13
+    public void setUp(){
14
+        introduction = "It was the best of times,\n" +
15
+                "it was the blurst of times,\n" +
16
+                "it was the age of wisdom,\n" +
17
+                "it was the age of foolishness,\n" +
18
+                "it was the epoch of belief,\n" +
19
+                "it was the epoch of incredulity,\n" +
20
+                "it was the season of Light,\n" +
21
+                "it was the season of Darkness,\n" +
22
+                "it was the spring of hope,\n" +
23
+                "it was the winter of despair,\n" +
24
+                "we had everything before us,\n" +
25
+                "we had nothing before us,\n" +
26
+                "we were all going direct to Heaven,\n" +
27
+                "we were all going direct the other way--\n" +
28
+                "in short, the period was so far like the present period, that some of\n" +
29
+                "its noisiest authorities insisted on its being received, for good or for\n" +
30
+                "evil, in the superlative degree of comparison only.";
31
+    }
32
+    @Test
33
+    public void run() {
34
+        SafeCopier safeCopier = new SafeCopier(introduction);
35
+        Thread monkeySafe1 = new Thread(safeCopier);
36
+        Thread monkeySafe2 = new Thread(safeCopier);
37
+        Thread monkeySafe3 = new Thread(safeCopier);
38
+        Thread monkeySafe4 = new Thread(safeCopier);
39
+        Thread monkeySafe5 = new Thread(safeCopier);
40
+
41
+        monkeySafe1.start();
42
+        monkeySafe2.start();
43
+        monkeySafe3.start();
44
+        monkeySafe4.start();
45
+        monkeySafe5.start();
46
+    }
47
+}

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

@@ -1,5 +1,7 @@
1 1
 package io.zipcoder;
2 2
 
3
+import java.util.ArrayList;
4
+
3 5
 public class MonkeyTypewriter {
4 6
     public static void main(String[] args) {
5 7
         String introduction = "It was the best of times,\n" +
@@ -24,6 +26,35 @@ public class MonkeyTypewriter {
24 26
         // For each Copier(one safe and one unsafe), create and start 5 monkeys copying the introduction to
25 27
         // A Tale Of Two Cities.
26 28
 
29
+        UnsafeCopier unsafeCopier = new UnsafeCopier(introduction);
30
+        Thread monkey1 = new Thread(unsafeCopier);
31
+        Thread monkey2 = new Thread(unsafeCopier);
32
+        Thread monkey3 = new Thread(unsafeCopier);
33
+        Thread monkey4 = new Thread(unsafeCopier);
34
+        Thread monkey5 = new Thread(unsafeCopier);
35
+
36
+        monkey1.start();
37
+        monkey2.start();
38
+        monkey3.start();
39
+        monkey4.start();
40
+        monkey5.start();
41
+
42
+        SafeCopier safeCopier = new SafeCopier(introduction);
43
+
44
+        ArrayList<Thread>monkeys = new ArrayList<Thread>();
45
+        Thread monkeySafe1 = new Thread(safeCopier);
46
+        Thread monkeySafe2 = new Thread(safeCopier);
47
+        Thread monkeySafe3 = new Thread(safeCopier);
48
+        Thread monkeySafe4 = new Thread(safeCopier);
49
+        Thread monkeySafe5 = new Thread(safeCopier);
50
+
51
+        monkeySafe1.start();
52
+        monkeySafe2.start();
53
+        monkeySafe3.start();
54
+        monkeySafe4.start();
55
+        monkeySafe5.start();
56
+
57
+
27 58
 
28 59
         // This wait is here because main is still a thread and we want the main method to print the finished copies
29 60
         // after enough time has passed.
@@ -34,5 +65,10 @@ public class MonkeyTypewriter {
34 65
         }
35 66
 
36 67
         // Print out the copied versions here.
68
+        System.out.println("----------Unsafe----------");
69
+        System.out.println(unsafeCopier.copied);
70
+        System.out.println("----------Safe----------");
71
+        System.out.println(safeCopier.copied);
72
+
37 73
     }
38 74
 }

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

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

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

@@ -10,5 +10,8 @@ public class UnsafeCopier extends Copier {
10 10
     }
11 11
 
12 12
     public void run() {
13
+        while (stringIterator.hasNext()) {
14
+            copied += stringIterator.next() + " ";
15
+        }
13 16
     }
14 17
 }