-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 312e53e
Showing
4 changed files
with
123 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
.DS_Store | ||
/bin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |