ソースを参照

Merge 6522e5a6ffdbcb590ca175d06ec69106924d51c7 into dbc7033eb3d6c33c19985384bea0b1a977cc30d3

carolynnmarie 6 年 前
コミット
f9bd4ad147
コミット者のEメールアドレスに関連付けられたアカウントが存在しません
共有3 個のファイルを変更した68 個の追加5 個の削除を含む
  1. 35
    4
      src/main/java/io/zipcoder/MonkeyTypewriter.java
  2. 22
    1
      src/main/java/io/zipcoder/SafeCopier.java
  3. 11
    0
      src/main/java/io/zipcoder/UnsafeCopier.java

+ 35
- 4
src/main/java/io/zipcoder/MonkeyTypewriter.java ファイルの表示

1
 package io.zipcoder;
1
 package io.zipcoder;
2
 
2
 
3
-public class MonkeyTypewriter {
4
-    public static void main(String[] args) {
3
+
4
+public class MonkeyTypewriter{
5
+
6
+    public static void main(String[] args) throws InterruptedException {
5
         String introduction = "It was the best of times,\n" +
7
         String introduction = "It was the best of times,\n" +
6
-                "it was the blurst of times,\n" +
8
+                "it was the worst of times,\n" +
7
                 "it was the age of wisdom,\n" +
9
                 "it was the age of wisdom,\n" +
8
                 "it was the age of foolishness,\n" +
10
                 "it was the age of foolishness,\n" +
9
                 "it was the epoch of belief,\n" +
11
                 "it was the epoch of belief,\n" +
20
                 "its noisiest authorities insisted on its being received, for good or for\n" +
22
                 "its noisiest authorities insisted on its being received, for good or for\n" +
21
                 "evil, in the superlative degree of comparison only.";
23
                 "evil, in the superlative degree of comparison only.";
22
 
24
 
25
+
26
+
23
         // Do all of the Monkey / Thread building here
27
         // 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
28
         // For each Copier(one safe and one unsafe), create and start 5 monkeys copying the introduction to
25
-        // A Tale Of Two Cities.
29
+        // A Tale Of Two Cities by Charles Dickens.
30
+
31
+        UnsafeCopier notSafe = new UnsafeCopier(introduction);
32
+        Thread[] notSafeThreads = new Thread[5];
33
+        for(int i=0; i<notSafeThreads.length; i++){
34
+            notSafeThreads[i] = new Thread(notSafe);
35
+            notSafeThreads[i].start();
36
+        }
37
+        for(Thread threads: notSafeThreads){
38
+            threads.join();
39
+        }
40
+
41
+
42
+        SafeCopier safe = new SafeCopier(introduction);
43
+        Thread[] safeThread = new Thread[5];
44
+        for(int i = 0; i<safeThread.length; i++){
45
+            safeThread[i] = new Thread(safe);
46
+            safeThread[i].start();
47
+        }
48
+        for(Thread threads: safeThread){
49
+            threads.join();
50
+       }
26
 
51
 
27
 
52
 
28
         // This wait is here because main is still a thread and we want the main method to print the finished copies
53
         // This wait is here because main is still a thread and we want the main method to print the finished copies
34
         }
59
         }
35
 
60
 
36
         // Print out the copied versions here.
61
         // Print out the copied versions here.
62
+        System.out.println(notSafe.copied);
63
+        System.out.println("____________________________________________");
64
+        System.out.println(safe.copied);
65
+
66
+
37
     }
67
     }
68
+
38
 }
69
 }

+ 22
- 1
src/main/java/io/zipcoder/SafeCopier.java ファイルの表示

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

+ 11
- 0
src/main/java/io/zipcoder/UnsafeCopier.java ファイルの表示

9
         super(toCopy);
9
         super(toCopy);
10
     }
10
     }
11
 
11
 
12
+
12
     public void run() {
13
     public void run() {
14
+        try {
15
+            Thread.sleep(30);
16
+        } catch(InterruptedException e) {
17
+            System.out.println("MAIN INTERRUPTED");
18
+        }
19
+        while(stringIterator.hasNext()){
20
+           copied += stringIterator.next() + " ";
21
+        }
13
     }
22
     }
23
+
14
 }
24
 }
25
+