瀏覽代碼

Merge 95154666bd519cd95a63dfec5d0adc80bad0da81 into dbc7033eb3d6c33c19985384bea0b1a977cc30d3

Haysel Giovanni Santiago 6 年之前
父節點
當前提交
4aec3b60a2
沒有帳戶連結到提交者的電子郵件

+ 32
- 0
src/main/java/io/zipcoder/MonkeyTypewriter.java 查看文件

@@ -1,5 +1,7 @@
1 1
 package io.zipcoder;
2 2
 
3
+import java.util.concurrent.ScheduledThreadPoolExecutor;
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,9 +26,37 @@ 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
+        //creation of unsafe threads with the object being passed into; additionally starting threads
30
+        UnsafeCopier unsafeCopier = new UnsafeCopier(introduction);
31
+
32
+        Thread UnsafeMonkeys1 = new Thread(unsafeCopier);
33
+        Thread UnsafeMonkeys2 = new Thread(unsafeCopier);
34
+        Thread UnsafeMonkeys3 = new Thread(unsafeCopier);
35
+        Thread UnsafeMonkeys4 = new Thread(unsafeCopier);
36
+        Thread UnsafeMonkeys5 = new Thread(unsafeCopier);
37
+
38
+      UnsafeMonkeys1.start();
39
+      UnsafeMonkeys2.start();
40
+      UnsafeMonkeys3.start();
41
+      UnsafeMonkeys4.start();
42
+      UnsafeMonkeys5.start();
43
+
44
+        SafeCopier safeCopier = new SafeCopier(introduction);
45
+        Thread SafeMonkeys1 = new Thread(safeCopier);
46
+        Thread SafeMonkeys2 = new Thread(safeCopier);
47
+        Thread SafeMonkeys3 = new Thread(safeCopier);
48
+        Thread SafeMonkeys4 = new Thread(safeCopier);
49
+        Thread SafeMonkeys5 = new Thread(safeCopier);
50
+
51
+        SafeMonkeys1.start();
52
+        SafeMonkeys2.start();
53
+        SafeMonkeys3.start();
54
+        SafeMonkeys4.start();
55
+        SafeMonkeys5.start();
27 56
 
28 57
         // This wait is here because main is still a thread and we want the main method to print the finished copies
29 58
         // after enough time has passed.
59
+
30 60
         try {
31 61
             Thread.sleep(1000);
32 62
         } catch(InterruptedException e) {
@@ -34,5 +64,7 @@ public class MonkeyTypewriter {
34 64
         }
35 65
 
36 66
         // Print out the copied versions here.
67
+        System.out.println(safeCopier.copied);
68
+        System.out.println(unsafeCopier.copied);
37 69
     }
38 70
 }

+ 30
- 2
src/main/java/io/zipcoder/SafeCopier.java 查看文件

@@ -1,8 +1,36 @@
1 1
 package io.zipcoder;
2 2
 
3
+import java.util.NoSuchElementException;
4
+import java.util.concurrent.TimeUnit;
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 lock1 = new ReentrantLock();
15
+
16
+    public SafeCopier(String toCopy) {
17
+        super(toCopy);
18
+    }
19
+
20
+    public void run() {
21
+        try {
22
+            TimeUnit.MILLISECONDS.sleep(1000);
23
+        } catch (InterruptedException e) {
24
+            e.printStackTrace();
25
+        }
26
+        while (stringIterator.hasNext()) {
27
+            lock1.lock();
28
+            try {
29
+                copied += stringIterator.next() + " " + Thread.currentThread().getName();
30
+            } catch (NoSuchElementException NSEE) {
31
+            } finally {
32
+                lock1.unlock();
33
+            }
34
+        }
35
+    }
36
+}

+ 3
- 1
src/main/java/io/zipcoder/UnsafeCopier.java 查看文件

@@ -4,11 +4,13 @@ package io.zipcoder;
4 4
  * Modify the run function so that the monkeys each grab the next word and write it to the copy.
5 5
  */
6 6
 public class UnsafeCopier extends Copier {
7
-
8 7
     public UnsafeCopier(String toCopy) {
9 8
         super(toCopy);
10 9
     }
11 10
 
12 11
     public void run() {
12
+        while(stringIterator.hasNext()){
13
+            copied += stringIterator.next() + " ";
14
+        }
13 15
     }
14 16
 }