-2

Scenario: I have a server that distributes summation problem to registered calculators.

The problem is that when server starts calculators remotely, he executes them inorder (wait for each one to return) although the calculation inside calculator should be done in a separate thread (thus it should return immediately).

Moreover, when I try to get type of current executing thread inside the calculator by parsing it to my thread type, I get error... and if I executed wait inside that thread, server thread stops instead.

Here is my code:

method inside server that solves a problem (this method is also called remotely by a client but I don't think this is related to what is happening)

@Override
public void solveProblem(Agent a, IClient client) throws RemoteException {
    Runnable thread = new Runnable(){
        @Override
        public void run()
        {
                //some variable initialization...
                for(int i=0;i<calcs.size();i++)
                {
                    try {
                        calcs.get(i).delegate(a, chosen);
                        System.out.println("delegate to "+i+" done");
                    } catch (RemoteException e) {
                        e.printStackTrace();
                    }
                }
         }
    };
}

calculator: delegate method initialises new thread and start calculation

@Override
public void delegate(Agent a, ICalculator original) throws RemoteException {
    receivedCount=0;
    thread = new CalculationThread(a, original);
    thread.run();
}

CalculationThread is an inner class within CalculatorImpl Class

class CalculationThread extends Thread{
    private Agent a;
    private ICalculator original;
    public String name = "Calculation Thread";
    public CalculationThread(Agent a, ICalculator original) {
        this.a=a;
        this.original=original;
    }
    @Override
    public String toString()
    {
        return "Original Calculation Thread";
    }
    @Override
    public void run()
    {
        //start calculation
        System.out.println("----start new query----");
        double result = 0;
        for(double n=a.lowerBound;n<=a.upperBound;n++)
        {
            result += Math.pow(-1, n)/(2*n+1);
        }
        result = result*4;
        System.out.println("Finished with result("+a.lowerBound+","+a.upperBound+"):"+result);

    }
}

Any body can explain what's the problem here?

A. Harkous
  • 252
  • 1
  • 3
  • 14
  • You gotta show some client code, also I am wondering how and what calculation results you are returning to the client? – hagrawal7777 Jun 19 '17 at 15:53
  • CalculationThread after finishing calculation it send the result to the original calculator (one chosen randomly by the server on each query) which in its turn uses the callback reference to client to show the result, this for "how", and for "what" I'am returning the total sum of the assigned problem. I ommited this part to avoid distraction as they are unrelated. I can add them if you think it could help? – A. Harkous Jun 19 '17 at 15:57
  • 1
    @EJP has caught the silly issue you had, check his answer. – hagrawal7777 Jun 19 '17 at 17:42

1 Answers1

3
thread.run();

You should be calling start(), not run(). At present you're just running the code in the current thread.

user207421
  • 305,947
  • 44
  • 307
  • 483