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();
    }
}


import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
public class SquareKaro
{
    public static void main(String[] args)
    {
        BlockingQueue<Integer> number = new LinkedBlockingQueue<>();
        BlockingQueue<SquareResult> result = new LinkedBlockingQueue<>();
        Squarer mysquarer = new Squarer(number, result);
        mysquarer.start();
        try
            {
                System.out.println("Hey another process, give me square of => 54");
                number.put(54);
                System.out.println(result.take());
            }
        catch(Exception e)
            {
                e.printStackTrace();
            }
    }
}


public class SquareResult
{
    private int input;
    private int output;
    public SquareResult(int in, int out)
    {
        this.input = in;
        this.output = out;
    }

    @Override public String toString()
    {
        return input +" ka square hai "+output;
    }
}
