瀏覽代碼

Added the skeleton for the concurrency lab

Zach Marcin 7 年之前
當前提交
732ad40da0

+ 3
- 0
.gitignore 查看文件

@@ -0,0 +1,3 @@
1
+.idea/*
2
+*.iml
3
+target/

+ 36
- 0
README.md 查看文件

@@ -0,0 +1,36 @@
1
+# TC-Concurrency
2
+
3
+## Monkey Typewriter
4
+According to Wikipedia:
5
+
6
+> The infinite monkey theorem states that a monkey hitting keys at random on a typewriter keyboard for an infinite 
7
+amount of time will almost surely type a given text.
8
+
9
+We don't have that kind of time, but what we do have are super smart monkeys.  These monkeys are able to copy text.
10
+
11
+So, guess what.  We're starting a printing company powered entirely off of monkey typists.
12
+
13
+### What to do!
14
+Testing multithreaded applications is super difficult.  Even more so, there's a chance that (if you're not actually
15
+testing things correctly) your tests will occasionally pass when they shouldn't (since a poorly threaded application
16
+isn't guaranteed to mess anything up).  Instead, we're going to use the main method in `Monkey Typewriter` to see
17
+exactly what happens when things are threaded incorrectly vs correctly.
18
+
19
+### Part 1
20
+
21
+Made for you is an abstract base class of `Copier` which has a constructor that takes a String and turns that into
22
+an iterator.  This will allow us to traverse the text to be copied and pass it along to each monkey (thread).
23
+
24
+Extend `Copier` in `UnsafeCopier`.  Then, write a `run` method that will have the monkey grab the next word and append
25
+it to the copy.
26
+
27
+Modify `MonkeyTypewriter` to create 5 monkeys (threads) using the `UnsafeCopier` and start them.
28
+
29
+After the sleep, print out the results of the unsafely copied passage.
30
+
31
+### Part 2
32
+
33
+Finish the `SafeCopier` and then call that from the main method, in addition to the unsafe version.
34
+
35
+
36
+

+ 19
- 0
pom.xml 查看文件

@@ -0,0 +1,19 @@
1
+<?xml version="1.0" encoding="UTF-8"?>
2
+<project xmlns="http://maven.apache.org/POM/4.0.0"
3
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5
+    <modelVersion>4.0.0</modelVersion>
6
+
7
+    <groupId>io.zipcoder</groupId>
8
+    <artifactId>TC-Concurrency</artifactId>
9
+    <version>1.0-SNAPSHOT</version>
10
+
11
+    <dependencies>
12
+        <dependency>
13
+            <groupId>junit</groupId>
14
+            <artifactId>junit</artifactId>
15
+            <version>4.12</version>
16
+            <scope>test</scope>
17
+        </dependency>
18
+    </dependencies>
19
+</project>

+ 21
- 0
src/main/java/io/zipcoder/Copier.java 查看文件

@@ -0,0 +1,21 @@
1
+package io.zipcoder;
2
+
3
+import java.util.Arrays;
4
+import java.util.Iterator;
5
+
6
+/**
7
+ * We're using this as an abstract base class since both the safe and unsafe copiers will implement it.
8
+ */
9
+public abstract class Copier implements Runnable {
10
+    // We use an iterator so each monkey / thread can copy an individual word.
11
+    public Iterator<String> stringIterator;
12
+    public String copied;
13
+
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.
16
+        this.stringIterator = Arrays.asList(toCopy.split(" ")).iterator();
17
+        this.copied = "";
18
+    }
19
+
20
+    public abstract void run();
21
+}

+ 38
- 0
src/main/java/io/zipcoder/MonkeyTypewriter.java 查看文件

@@ -0,0 +1,38 @@
1
+package io.zipcoder;
2
+
3
+public class MonkeyTypewriter {
4
+    public static void main(String[] args) {
5
+        String introduction = "It was the best of times,\n" +
6
+                "it was the blurst of times,\n" +
7
+                "it was the age of wisdom,\n" +
8
+                "it was the age of foolishness,\n" +
9
+                "it was the epoch of belief,\n" +
10
+                "it was the epoch of incredulity,\n" +
11
+                "it was the season of Light,\n" +
12
+                "it was the season of Darkness,\n" +
13
+                "it was the spring of hope,\n" +
14
+                "it was the winter of despair,\n" +
15
+                "we had everything before us,\n" +
16
+                "we had nothing before us,\n" +
17
+                "we were all going direct to Heaven,\n" +
18
+                "we were all going direct the other way--\n" +
19
+                "in short, the period was so far like the present period, that some of\n" +
20
+                "its noisiest authorities insisted on its being received, for good or for\n" +
21
+                "evil, in the superlative degree of comparison only.";
22
+
23
+        // Do all of the Monkey / Thread building here
24
+        // For each Copier(one safe and one unsafe), create and start 5 monkeys copying the introduction to
25
+        // A Tale Of Two Cities.
26
+
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");
34
+        }
35
+
36
+        // Print out the copied versions here.
37
+    }
38
+}

+ 8
- 0
src/main/java/io/zipcoder/SafeCopier.java 查看文件

@@ -0,0 +1,8 @@
1
+package io.zipcoder;
2
+
3
+/**
4
+ * Make this extend the Copier like `UnsafeCopier`, except use locks to make sure that the actual intro gets printed
5
+ * correctly every time.  Make the run method thread safe.
6
+ */
7
+public class SafeCopier {
8
+}

+ 14
- 0
src/main/java/io/zipcoder/UnsafeCopier.java 查看文件

@@ -0,0 +1,14 @@
1
+package io.zipcoder;
2
+
3
+/**
4
+ * Modify the run function so that the monkeys each grab the next word and write it to the copy.
5
+ */
6
+public class UnsafeCopier extends Copier {
7
+
8
+    public UnsafeCopier(String toCopy) {
9
+        super(toCopy);
10
+    }
11
+
12
+    public void run() {
13
+    }
14
+}