Ver código fonte

Merge c96d7bc3c7ab831034e6be11834abcb099f29286 into dbc7033eb3d6c33c19985384bea0b1a977cc30d3

April Rivera 6 anos atrás
pai
commit
67b8204499
Nenhuma conta conectada ao e-mail do autor de commit

+ 2
- 1
src/main/java/io/zipcoder/Copier.java Ver arquivo

@@ -12,10 +12,11 @@ public abstract class Copier implements Runnable {
12 12
     public String copied;
13 13
 
14 14
     public Copier(String toCopy) {
15
-        // Take the input string, split it on spaces, turn that array to an arraylist, and then grab its iterator.
15
+        // Take the input string, split it on spaces, turn that array to an arrayList, and then grab its iterator.
16 16
         this.stringIterator = Arrays.asList(toCopy.split(" ")).iterator();
17 17
         this.copied = "";
18 18
     }
19 19
 
20 20
     public abstract void run();
21
+
21 22
 }

+ 18
- 2
src/main/java/io/zipcoder/MonkeyTypewriter.java Ver arquivo

@@ -23,7 +23,22 @@ public class MonkeyTypewriter {
23 23
         // Do all of the Monkey / Thread building here
24 24
         // For each Copier(one safe and one unsafe), create and start 5 monkeys copying the introduction to
25 25
         // A Tale Of Two Cities.
26
-
26
+        UnsafeCopier unsafe = new UnsafeCopier(introduction);
27
+        Thread[] threads = new Thread[5];
28
+        for(int i = 0; i < threads.length; i++){
29
+            threads[i] = new Thread(unsafe);
30
+        }
31
+        for(Thread t : threads){
32
+            t.start();
33
+        }
34
+        SafeCopier safeCopier = new SafeCopier(introduction);
35
+        Thread[] threads2 = new Thread[5];
36
+        for(int i = 0; i < threads2.length; i++){
37
+             threads2[i] = new Thread(safeCopier);
38
+        }
39
+        for(Thread t2 : threads2){
40
+            t2.start();
41
+        }
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.
@@ -32,7 +47,8 @@ public class MonkeyTypewriter {
32 47
         } catch(InterruptedException e) {
33 48
             System.out.println("MAIN INTERRUPTED");
34 49
         }
35
-
50
+        System.out.println(unsafe.copied);
51
+        System.out.println(safeCopier.copied);
36 52
         // Print out the copied versions here.
37 53
     }
38 54
 }

+ 27
- 1
src/main/java/io/zipcoder/SafeCopier.java Ver arquivo

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

+ 18
- 1
src/main/java/io/zipcoder/UnsafeCopier.java Ver arquivo

@@ -1,14 +1,31 @@
1 1
 package io.zipcoder;
2 2
 
3
+import java.util.Iterator;
4
+
3 5
 /**
4 6
  * Modify the run function so that the monkeys each grab the next word and write it to the copy.
5 7
  */
6 8
 public class UnsafeCopier extends Copier {
7
-
9
+    // We use an iterator so each monkey / thread can copy an individual word.
10
+//    public Iterator<String> stringIterator;
11
+//    public String copied;
8 12
     public UnsafeCopier(String toCopy) {
9 13
         super(toCopy);
10 14
     }
11 15
 
12 16
     public void run() {
17
+        try {
18
+            Thread.sleep(60);
19
+        } catch (InterruptedException e) {
20
+            e.printStackTrace();
21
+        }
22
+        if (stringIterator.hasNext()) {
23
+            StringBuilder sb = new StringBuilder();
24
+            String s = stringIterator.next();
25
+            sb.append(s);
26
+
27
+            copied = copied + sb.toString() + " " + Thread.currentThread().getName();
28
+        }
13 29
     }
14 30
 }
31
+