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?
thread.run();
You should be calling start()
, not run()
. At present you're just running the code in the current thread.