programming-examples/java/Squarer.java

45 lines
1.2 KiB
Java
Raw Normal View History

2019-11-18 13:43:20 +01:00
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();
}
}