Преглед на файлове

Merge a1968440f72aaff35bcfdf873768d2d3a2945cfb into dbc7033eb3d6c33c19985384bea0b1a977cc30d3

Patrick Glavin преди 6 години
родител
ревизия
ac89a6e554
No account linked to committer's email

+ 1
- 1
pom.xml Целия файл

@@ -12,7 +12,7 @@
12 12
         <dependency>
13 13
             <groupId>junit</groupId>
14 14
             <artifactId>junit</artifactId>
15
-            <version>4.12</version>
15
+            <version>RELEASE</version>
16 16
             <scope>test</scope>
17 17
         </dependency>
18 18
     </dependencies>

+ 4
- 0
src/main/java/io/zipcoder/Copier.java Целия файл

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

+ 25
- 6
src/main/java/io/zipcoder/MonkeyTypewriter.java Целия файл

@@ -25,14 +25,33 @@ public class MonkeyTypewriter {
25 25
         // A Tale Of Two Cities.
26 26
 
27 27
 
28
-        // This wait is here because main is still a thread and we want the main method to print the finished copies
29
-        // after enough time has passed.
30
-        try {
31
-            Thread.sleep(1000);
32
-        } catch(InterruptedException e) {
33
-            System.out.println("MAIN INTERRUPTED");
28
+        Copier unsafeCopier = new UnsafeCopier(introduction);
29
+        Thread[] unsafeThreads = new Thread[] {new Thread(unsafeCopier), new Thread(unsafeCopier), new Thread(unsafeCopier), new Thread(unsafeCopier), new Thread(unsafeCopier)};
30
+        for (Thread thread : unsafeThreads) {
31
+            thread.start();
34 32
         }
35 33
 
34
+        Copier safeCopier = new SafeCopier(introduction);
35
+        Thread[] safeThreads = new Thread[] {new Thread(safeCopier), new Thread(safeCopier), new Thread(safeCopier), new Thread(safeCopier), new Thread(safeCopier)};
36
+        for (Thread thread : safeThreads) {
37
+            thread.start();
38
+        }
39
+//        for (int j = 0; j < safeArray.length; j++) safeArray[j].start();
40
+
41
+
42
+
43
+        for(Thread thread : safeThreads) {
44
+            try {
45
+                thread.join();
46
+            } catch (InterruptedException e) {
47
+                e.printStackTrace();
48
+            }
49
+        }
50
+
51
+
36 52
         // Print out the copied versions here.
53
+        System.out.println(unsafeCopier.copied);
54
+        System.out.println("---------------------------------------");
55
+        System.out.println(safeCopier.copied);
37 56
     }
38 57
 }

+ 27
- 1
src/main/java/io/zipcoder/SafeCopier.java Целия файл

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

+ 9
- 0
src/main/java/io/zipcoder/UniqueThread.java Целия файл

@@ -0,0 +1,9 @@
1
+package io.zipcoder;
2
+
3
+public class UniqueThread extends Thread{
4
+//    private int id = 0;
5
+//
6
+//    public UniqueThread(){
7
+//        this.id =
8
+//    }
9
+}

+ 13
- 0
src/main/java/io/zipcoder/UnsafeCopier.java Целия файл

@@ -1,5 +1,7 @@
1 1
 package io.zipcoder;
2 2
 
3
+import java.util.concurrent.TimeUnit;
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
  */
@@ -10,5 +12,16 @@ public class UnsafeCopier extends Copier {
10 12
     }
11 13
 
12 14
     public void run() {
15
+        try {
16
+            TimeUnit.MILLISECONDS.sleep(30);
17
+        } catch (InterruptedException e) {
18
+            e.printStackTrace();
19
+        }
20
+        while (this.stringIterator.hasNext()) {
21
+            String s = stringIterator.next();
22
+            StringBuilder sb = new StringBuilder();
23
+            sb.append(s);
24
+            this.copied += sb.toString() + " " + Thread.currentThread().getName() + " ";
25
+        }
13 26
     }
14 27
 }