Fixed race condition

pull/50/head
M66B 6 years ago
parent badbd134f5
commit afb7668d6e

@ -72,6 +72,7 @@ import java.util.Properties;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.RejectedExecutionException; import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.Semaphore;
import javax.mail.Address; import javax.mail.Address;
import javax.mail.FetchProfile; import javax.mail.FetchProfile;
@ -1557,31 +1558,25 @@ public class ServiceSynchronize extends LifecycleService {
public static void stopSynchronous(Context context, String reason) { public static void stopSynchronous(Context context, String reason) {
Log.i(Helper.TAG, "Stop because of '" + reason + "'"); Log.i(Helper.TAG, "Stop because of '" + reason + "'");
final Object lock = new Object(); final Semaphore semaphore = new Semaphore(0, true);
ServiceConnection connection = new ServiceConnection() { ServiceConnection connection = new ServiceConnection() {
@Override @Override
public void onServiceConnected(ComponentName componentName, IBinder binder) { public void onServiceConnected(ComponentName componentName, IBinder binder) {
Log.i(Helper.TAG, "Service connected"); Log.i(Helper.TAG, "Service connected");
((LocalBinder) binder).getService().quit(); ((LocalBinder) binder).getService().quit();
synchronized (lock) { semaphore.release();
lock.notify();
}
} }
@Override @Override
public void onServiceDisconnected(ComponentName componentName) { public void onServiceDisconnected(ComponentName componentName) {
Log.i(Helper.TAG, "Service disconnected"); Log.i(Helper.TAG, "Service disconnected");
synchronized (lock) { semaphore.release();
lock.notify();
}
} }
@Override @Override
public void onBindingDied(ComponentName name) { public void onBindingDied(ComponentName name) {
Log.i(Helper.TAG, "Service died"); Log.i(Helper.TAG, "Service died");
synchronized (lock) { semaphore.release();
lock.notify();
}
} }
}; };
@ -1592,9 +1587,7 @@ public class ServiceSynchronize extends LifecycleService {
if (exists) { if (exists) {
Log.i(Helper.TAG, "Service stopping"); Log.i(Helper.TAG, "Service stopping");
try { try {
synchronized (lock) { semaphore.acquire();
lock.wait();
}
} catch (InterruptedException ex) { } catch (InterruptedException ex) {
Log.e(Helper.TAG, ex + "\n" + Log.getStackTraceString(ex)); Log.e(Helper.TAG, ex + "\n" + Log.getStackTraceString(ex));
} }

Loading…
Cancel
Save