Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
maxg committed Apr 5, 2017
0 parents commit 312e53e
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="output" path="bin"/>
</classpath>
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.DS_Store
/bin
17 changes: 17 additions & 0 deletions .project
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>ex22-square-queue</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
98 changes: 98 additions & 0 deletions src/square/SquareQueue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package square;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

/**
* Squares integers.
*/
class Squarer {

private final BlockingQueue<Integer> in;
private final BlockingQueue<SquareResult> out;
// Rep invariant: in, out != null

/**
* Make a squarer that will listen for requests and generate replies.
* @param requests queue to receive requests from
* @param replies queue to send replies to
*/
public Squarer(BlockingQueue<Integer> requests, BlockingQueue<SquareResult> replies) {
this.in = requests;
this.out = replies;
}

/**
* Start handling squaring requests.
*/
public void start() {
new Thread(new Runnable() {
public void run() {
while (true) {
// TODO: we may want a way to stop the thread
try {
// block until a request arrives
int x = in.take();
// compute the answer and send it back
int y = x * x;
out.put(new SquareResult(x, y));
} catch (InterruptedException ie) {
ie.printStackTrace();
}
}
}
}).start();
}
}

/**
* An immutable squaring result message.
*/
class SquareResult {
private final int input;
private final int output;

/**
* Make a new result message.
* @param input input number
* @param output square of input
*/
public SquareResult(int input, int output) {
this.input = input;
this.output = output;
}

// TODO: we will want more observers, but for now...

@Override public String toString() {
return input + "^2 = " + output;
}
}

public class SquareQueue {

/**
* Create and use a squarer.
* @param args unused
*/
public static void main(String[] args) {

BlockingQueue<Integer> requests = new LinkedBlockingQueue<>();
BlockingQueue<SquareResult> replies = new LinkedBlockingQueue<>();

Squarer squarer = new Squarer(requests, replies);
squarer.start();

try {
// make a request
requests.put(42);

// maybe do something concurrently...

// read the reply
System.out.println(replies.take());
} catch (InterruptedException ie) {
ie.printStackTrace();
}
}
}

0 comments on commit 312e53e

Please sign in to comment.