Selaa lähdekoodia

added some additional monkey chaos

Eric Barnaba 6 vuotta sitten
vanhempi
commit
7f0551bc2f

+ 27
- 10
src/main/java/io/zipcoder/MonkeyTypewriter.java Näytä tiedosto

@@ -31,12 +31,12 @@ public class MonkeyTypewriter {
31 31
         Thread monkeyMichelle = new Thread(unsafeCopy, "[4]");
32 32
         Thread monkeyHeather = new Thread(unsafeCopy, "[5]");
33 33
 
34
+        Thread[] unsafeThreads = {monkeySteve,monkeyGuido,monkeyJon,monkeyMichelle,monkeyHeather};
35
+
36
+        for(Thread t : unsafeThreads){
37
+            t.start();
38
+        }
34 39
 
35
-            monkeySteve.start();
36
-            monkeyGuido.start();
37
-            monkeyJon.start();
38
-            monkeyMichelle.start();
39
-            monkeyHeather.start();
40 40
 
41 41
 
42 42
         SafeCopier safeCopy = new SafeCopier(introduction);
@@ -47,11 +47,24 @@ public class MonkeyTypewriter {
47 47
         Thread monkeyAmy = new Thread(safeCopy,"[9]");
48 48
         Thread monkeyDan = new Thread(safeCopy,"[10]");
49 49
 
50
-        monkeyJoe.start();
51
-        monkeyVince.start();
52
-        monkeyKibret.start();
53
-        monkeyAmy.start();
54
-        monkeyDan.start();
50
+        Thread[] safeThreads = {monkeyJoe,monkeyVince,monkeyKibret,monkeyAmy,monkeyDan};
51
+        long startTime = System.currentTimeMillis();
52
+        for(Thread t : safeThreads){
53
+            t.start();
54
+        }
55
+
56
+        for(Thread t : safeThreads){
57
+            try {
58
+                t.join();
59
+            }
60
+            catch(InterruptedException e){
61
+                System.out.println("MONKEY INTERRUPTED");
62
+            }
63
+        }
64
+
65
+        long duration = System.currentTimeMillis()-startTime;
66
+
67
+
55 68
 
56 69
 
57 70
 
@@ -67,6 +80,10 @@ public class MonkeyTypewriter {
67 80
         System.out.println("Unsafe Copy: \n" + unsafeCopy.copied);
68 81
         System.out.println();
69 82
 
83
+
84
+
85
+
86
+        System.out.println("Multi-threaded process computed in " + duration + "ms");
70 87
         System.out.println("Safe Copy: \n" + safeCopy.copied);
71 88
     }
72 89
 }

+ 8
- 8
src/main/java/io/zipcoder/SafeCopier.java Näytä tiedosto

@@ -19,19 +19,19 @@ public class SafeCopier extends Copier {
19 19
     }
20 20
 
21 21
 
22
-//    public synchronized void run() {
23
-//        while(stringIterator.hasNext()){
24
-//            this.copied = this.copied + stringIterator.next() + " " + Thread.currentThread().getName();
25
-//        }
26
-//
27
-//    }
28
-
29 22
     public void run() {
23
+        try{
24
+            Thread.sleep(50);
25
+        }
26
+        catch(InterruptedException e){
27
+            System.out.println("SLEEP INTERRUPTED");
28
+        }
30 29
 
31 30
         while (stringIterator.hasNext()) {
32 31
             copyLock.lock();
32
+            StringBuilder builder = new StringBuilder();
33 33
             try {
34
-                this.copied += stringIterator.next() + " " + Thread.currentThread().getName();
34
+                this.copied += builder.append(stringIterator.next() + " " + Thread.currentThread().getName()).toString();
35 35
             } catch (NoSuchElementException e) {
36 36
 
37 37
             } finally {

+ 9
- 1
src/main/java/io/zipcoder/UnsafeCopier.java Näytä tiedosto

@@ -12,9 +12,17 @@ public class UnsafeCopier extends Copier{
12 12
     }
13 13
 
14 14
     public void run() {
15
+        try{
16
+            Thread.sleep(50);
17
+        }
18
+        catch(InterruptedException e){
19
+            System.out.println("SLEEP INTERRUPTED");
20
+        }
21
+
15 22
         while(stringIterator.hasNext()){
23
+            StringBuilder builder = new StringBuilder();
16 24
             try {
17
-                this.copied = this.copied + stringIterator.next() + " " + Thread.currentThread().getName();
25
+                this.copied += builder.append(stringIterator.next() + " " + Thread.currentThread().getName()).toString();
18 26
             }
19 27
             catch(NoSuchElementException e){
20 28