#18 KennethT867 ThreadsTypewriteres Lab

Atvērta
KennethT867 vēlas sapludināt 1 revīzijas no KennethT867/ZCW-MonkeysTypewritersOhMy:master uz master

+ 2
- 0
src/main/java/io/zipcoder/Copier.java Parādīt failu

@@ -1,5 +1,6 @@
1 1
 package io.zipcoder;
2 2
 
3
+import java.util.ArrayList;
3 4
 import java.util.Arrays;
4 5
 import java.util.Iterator;
5 6
 
@@ -18,4 +19,5 @@ public abstract class Copier implements Runnable {
18 19
     }
19 20
 
20 21
     public abstract void run();
22
+
21 23
 }

+ 33
- 0
src/main/java/io/zipcoder/MonkeyTypewriter.java Parādīt failu

@@ -1,6 +1,21 @@
1 1
 package io.zipcoder;
2 2
 
3 3
 public class MonkeyTypewriter {
4
+
5
+
6
+    /*
7
+    Unsafe threads work, but commented out for optimization.
8
+     */
9
+
10
+
11
+
12
+
13
+
14
+
15
+
16
+
17
+
18
+
4 19
     public static void main(String[] args) {
5 20
         String introduction = "It was the best of times,\n" +
6 21
                 "it was the blurst of times,\n" +
@@ -27,12 +42,30 @@ public class MonkeyTypewriter {
27 42
 
28 43
         // This wait is here because main is still a thread and we want the main method to print the finished copies
29 44
         // after enough time has passed.
45
+        UnsafeCopier unsafeCopier0 = new UnsafeCopier(introduction);
46
+        SafeCopier safeCopier0 = new SafeCopier(introduction);
30 47
         try {
48
+//            new Thread(unsafeCopier0).start();
49
+//            new Thread(unsafeCopier0).start();
50
+//            new Thread(unsafeCopier0).start();
51
+//            new Thread(unsafeCopier0).start();
52
+//            new Thread(unsafeCopier0).start();
53
+
54
+            new Thread(safeCopier0).start();
55
+            new Thread(safeCopier0).start();
56
+            new Thread(safeCopier0).start();
57
+            new Thread(safeCopier0).start();
58
+            new Thread(safeCopier0).start();
59
+
60
+
61
+
31 62
             Thread.sleep(1000);
32 63
         } catch(InterruptedException e) {
33 64
             System.out.println("MAIN INTERRUPTED");
34 65
         }
35 66
 
67
+        System.out.println(safeCopier0.copied);
36 68
         // Print out the copied versions here.
37 69
     }
70
+
38 71
 }

+ 21
- 1
src/main/java/io/zipcoder/SafeCopier.java Parādīt failu

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

+ 12
- 1
src/main/java/io/zipcoder/UnsafeCopier.java Parādīt failu

@@ -5,10 +5,21 @@ package io.zipcoder;
5 5
  */
6 6
 public class UnsafeCopier extends Copier {
7 7
 
8
+    StringBuilder x = new StringBuilder();
9
+
8 10
     public UnsafeCopier(String toCopy) {
9 11
         super(toCopy);
10 12
     }
11 13
 
12 14
     public void run() {
15
+        while(stringIterator.hasNext() == true){
16
+            String w = stringIterator.next();
17
+            copied = copied + w + " ";
18
+
19
+            //    x.append(w + " ");
20
+            //System.out.println(copied);
21
+        }
22
+        }
23
+
24
+       // System.out.println(x.toString());
13 25
     }
14
-}