Try to fix issue with route recalculation

This commit is contained in:
Victor Shcherb 2015-01-12 02:02:35 +01:00
parent ae338b90b0
commit 50804ae0ce

View file

@ -709,6 +709,7 @@ public class RoutingHelper {
private final RouteCalculationParams params; private final RouteCalculationParams params;
private boolean paramsChanged; private boolean paramsChanged;
private Thread prevRunningJob;
public RouteRecalculationThread(String name, RouteCalculationParams params, boolean paramsChanged) { public RouteRecalculationThread(String name, RouteCalculationParams params, boolean paramsChanged) {
super(name); super(name);
@ -730,10 +731,23 @@ public class RoutingHelper {
@Override @Override
public void run() { public void run() {
if(prevRunningJob != null) {
while(prevRunningJob.isAlive()){
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
synchronized (RoutingHelper.this) {
currentRunningJob = this;
}
RouteCalculationResult res = provider.calculateRouteImpl(params); RouteCalculationResult res = provider.calculateRouteImpl(params);
if (params.calculationProgress.isCancelled) { if (params.calculationProgress.isCancelled) {
currentRunningJob = null; synchronized (RoutingHelper.this) {
currentRunningJob = null;
}
return; return;
} }
final boolean onlineSourceWithoutInternet = !res.isCalculated() && params.type.isOnline() && !settings.isInternetConnectionAvailable(); final boolean onlineSourceWithoutInternet = !res.isCalculated() && params.type.isOnline() && !settings.isInternetConnectionAvailable();
@ -773,6 +787,10 @@ public class RoutingHelper {
} }
lastTimeEvaluatedRoute = System.currentTimeMillis(); lastTimeEvaluatedRoute = System.currentTimeMillis();
} }
public void setWaitPrevJob(Thread prevRunningJob) {
this.prevRunningJob = prevRunningJob;
}
} }
public void recalculateRouteDueToSettingsChange() { public void recalculateRouteDueToSettingsChange() {
@ -787,9 +805,9 @@ public class RoutingHelper {
if (start == null || end == null) { if (start == null || end == null) {
return; return;
} }
if (currentRunningJob == null) { if (currentRunningJob == null || force) {
// do not evaluate very often // do not evaluate very often
if (force || System.currentTimeMillis() - lastTimeEvaluatedRoute > evalWaitInterval) { if (System.currentTimeMillis() - lastTimeEvaluatedRoute > evalWaitInterval || force) {
RouteCalculationParams params = new RouteCalculationParams(); RouteCalculationParams params = new RouteCalculationParams();
params.start = start; params.start = start;
params.end = end; params.end = end;
@ -807,7 +825,12 @@ public class RoutingHelper {
updateProgress(params.calculationProgress); updateProgress(params.calculationProgress);
} }
synchronized (this) { synchronized (this) {
currentRunningJob = new RouteRecalculationThread("Calculating route", params, paramsChanged); //$NON-NLS-1$ final Thread prevRunningJob = currentRunningJob;
RouteRecalculationThread newThread = new RouteRecalculationThread("Calculating route", params, paramsChanged); //$NON-NLS-1$
currentRunningJob = newThread ;
if(prevRunningJob != null) {
newThread.setWaitPrevJob(prevRunningJob);
}
currentRunningJob.start(); currentRunningJob.start();
} }
} }