Sfoglia il codice sorgente

Merge 98dbe9c10a4a43862c20723f0c31cc534ce15d4d into dbc7033eb3d6c33c19985384bea0b1a977cc30d3

CWinarski 6 anni fa
parent
commit
2b7ee3b106
No account linked to committer's email

+ 31
- 0
src/main/java/io/zipcoder/MonkeyTypewriter.java Vedi File

@@ -24,6 +24,33 @@ public class MonkeyTypewriter {
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
 
27
+        Copier testCopier = new UnsafeCopier(introduction);
28
+        Thread monkeyThread1 = new Thread(testCopier);
29
+        Thread monkeyThread2 = new Thread(testCopier);
30
+        Thread monkeyThread3 = new Thread(testCopier);
31
+        Thread monkeyThread4 = new Thread(testCopier);
32
+        Thread monkeyThread5 = new Thread(testCopier);
33
+
34
+        monkeyThread1.start();
35
+        monkeyThread2.start();
36
+        monkeyThread3.start();
37
+        monkeyThread4.start();
38
+        monkeyThread5.start();
39
+
40
+        Copier testSafeCopier = new SafeCopier(introduction);
41
+        Thread monkeyEmille = new Thread(testSafeCopier);
42
+        Thread monkeyHenry= new Thread(testSafeCopier);
43
+        Thread monkeyGeorge = new Thread(testSafeCopier);
44
+        Thread monkeyBob = new Thread(testSafeCopier);
45
+        Thread monkeyAlex = new Thread(testSafeCopier);
46
+
47
+        monkeyEmille.start();
48
+        monkeyHenry.start();
49
+        monkeyGeorge.start();
50
+        monkeyBob.start();
51
+        monkeyAlex.start();
52
+
53
+
27 54
 
28 55
         // This wait is here because main is still a thread and we want the main method to print the finished copies
29 56
         // after enough time has passed.
@@ -34,5 +61,9 @@ public class MonkeyTypewriter {
34 61
         }
35 62
 
36 63
         // Print out the copied versions here.
64
+        System.out.println("------------UnsafeCopier------------");
65
+        System.out.println(testCopier.copied);
66
+        System.out.println("------------SafeCopier--------------");
67
+        System.out.println(testSafeCopier.copied);
37 68
     }
38 69
 }

+ 29
- 1
src/main/java/io/zipcoder/SafeCopier.java Vedi File

@@ -1,8 +1,36 @@
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
+    Lock sharedLock = new ReentrantLock();
13
+    public SafeCopier(String toCopy){
14
+        super(toCopy);
15
+    }
16
+
17
+    public void run() {
18
+        boolean keepGoing = true;
19
+        try {
20
+            TimeUnit.MILLISECONDS.sleep(30);
21
+        } catch (InterruptedException e){
22
+            e.printStackTrace();
23
+        }
24
+        while(keepGoing){
25
+            if(sharedLock.tryLock()){
26
+                if (stringIterator.hasNext()){
27
+                    copied += stringIterator.next() + " " + Thread.currentThread().getName() + " ";
28
+                } else keepGoing = false;
29
+                sharedLock.unlock();
30
+            }
31
+
32
+        }
33
+
34
+
35
+    }
8 36
 }

+ 4
- 0
src/main/java/io/zipcoder/UnsafeCopier.java Vedi File

@@ -10,5 +10,9 @@ public class UnsafeCopier extends Copier {
10 10
     }
11 11
 
12 12
     public void run() {
13
+        while (stringIterator.hasNext()) {
14
+            copied += stringIterator.next() + " ";
15
+        }
16
+
13 17
     }
14 18
 }

+ 41
- 0
src/test/java/io/zipcoder/SafeCopierTest.java Vedi File

@@ -0,0 +1,41 @@
1
+//package io.zipcoder;
2
+//
3
+//
4
+//import org.junit.Assert;
5
+//import org.junit.Before;
6
+//import org.junit.Test;
7
+//
8
+//public class SafeCopierTest {
9
+//    String intro;
10
+//    @Before
11
+//    public void setUp(){
12
+//        intro = "It was the best of times,\n" +
13
+//                "it was the blurst of times,\n" +
14
+//                "it was the age of wisdom,\n" +
15
+//                "it was the age of foolishness,\n" +
16
+//                "it was the epoch of belief,\n" +
17
+//                "it was the epoch of incredulity,\n" +
18
+//                "it was the season of Light,\n" +
19
+//                "it was the season of Darkness,\n" +
20
+//                "it was the spring of hope,\n" +
21
+//                "it was the winter of despair,\n" +
22
+//                "we had everything before us,\n" +
23
+//                "we had nothing before us,\n" +
24
+//                "we were all going direct to Heaven,\n" +
25
+//                "we were all going direct the other way--\n" +
26
+//                "in short, the period was so far like the present period, that some of\n" +
27
+//                "its noisiest authorities insisted on its being received, for good or for\n" +
28
+//                "evil, in the superlative degree of comparison only.";
29
+//
30
+//    }
31
+//    @Test
32
+//    public void safeCopyRunTest(){
33
+//        SafeCopier testCopier = new SafeCopier(intro);
34
+//        String expected = intro;
35
+//        String actual = testCopier.run();
36
+//
37
+//        Assert.assertEquals(expected,actual);
38
+//
39
+//    }
40
+//  YAY learned to make test directory!
41
+//}