45 lines
1.2 KiB
Java
45 lines
1.2 KiB
Java
import java.util.concurrent.BlockingQueue;
|
|
import java.util.concurrent.LinkedBlockingQueue;
|
|
|
|
public class Squarer
|
|
{
|
|
private BlockingQueue<Integer> in;
|
|
private BlockingQueue<SquareResult> out;
|
|
|
|
Squarer(BlockingQueue<Integer> request, BlockingQueue<SquareResult> replies)
|
|
{
|
|
this.in = request;
|
|
this.out = replies;
|
|
}
|
|
|
|
public void start()
|
|
{
|
|
new Thread(new Runnable()
|
|
{
|
|
public void run()
|
|
{
|
|
while(true)
|
|
{
|
|
try
|
|
{
|
|
int x = in.take();
|
|
int y = x*x;
|
|
Thread.sleep(2000);
|
|
System.out.println("OK, got it. Calculating..");
|
|
Thread.sleep(4000);
|
|
out.put(new SquareResult(x,y));
|
|
}
|
|
catch(InterruptedException e)
|
|
{
|
|
System.out.println("interrupted yo");
|
|
}
|
|
}
|
|
}
|
|
}).start();
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|