1
0
mirror of https://github.com/moparisthebest/k-9 synced 2024-11-16 06:25:06 -05:00

Fixes Issue 1441

Shutdown threadPool when Service is destroyed.
This commit is contained in:
Daniel Applebaum 2010-04-20 03:22:43 +00:00
parent 1afd99e6c8
commit 9310f3f6c6

View File

@ -20,8 +20,19 @@ public abstract class CoreService extends Service
public static String WAKE_LOCK_ID = "com.fsck.k9.service.CoreService.wakeLockId"; public static String WAKE_LOCK_ID = "com.fsck.k9.service.CoreService.wakeLockId";
private static ConcurrentHashMap<Integer, WakeLock> wakeLocks = new ConcurrentHashMap<Integer, WakeLock>(); private static ConcurrentHashMap<Integer, WakeLock> wakeLocks = new ConcurrentHashMap<Integer, WakeLock>();
private static AtomicInteger wakeLockSeq = new AtomicInteger(0); private static AtomicInteger wakeLockSeq = new AtomicInteger(0);
private final ExecutorService threadPool = Executors.newFixedThreadPool(1); // Must be single threaded private ExecutorService threadPool = null;
private final String className = getClass().getName();
@Override
public void onCreate()
{
if (K9.DEBUG)
Log.i(K9.LOG_TAG, "CoreService: " + className + ".onCreate()");
threadPool = Executors.newFixedThreadPool(1); // Must be single threaded
super.onCreate();
}
protected static void addWakeLockId(Intent i, Integer wakeLockId) protected static void addWakeLockId(Intent i, Integer wakeLockId)
{ {
if (wakeLockId != null) if (wakeLockId != null)
@ -42,6 +53,8 @@ public abstract class CoreService extends Service
i.putExtra(WAKE_LOCK_ID, tmpWakeLockId); i.putExtra(WAKE_LOCK_ID, tmpWakeLockId);
} }
@Override @Override
public void onStart(Intent intent, int startId) public void onStart(Intent intent, int startId)
@ -53,7 +66,7 @@ public abstract class CoreService extends Service
wakeLock.acquire(K9.MAIL_SERVICE_WAKE_LOCK_TIMEOUT); wakeLock.acquire(K9.MAIL_SERVICE_WAKE_LOCK_TIMEOUT);
if (K9.DEBUG) if (K9.DEBUG)
Log.i(K9.LOG_TAG, "CoreService: " + this.getClass().getName() + ".onStart(" + intent + ", " + startId); Log.i(K9.LOG_TAG, "CoreService: " + className + ".onStart(" + intent + ", " + startId);
int wakeLockId = intent.getIntExtra(BootReceiver.WAKE_LOCK_ID, -1); int wakeLockId = intent.getIntExtra(BootReceiver.WAKE_LOCK_ID, -1);
if (wakeLockId != -1) if (wakeLockId != -1)
@ -91,12 +104,12 @@ public abstract class CoreService extends Service
public void execute(Context context, final Runnable runner, int wakeLockTime, final Integer startId) public void execute(Context context, final Runnable runner, int wakeLockTime, final Integer startId)
{ {
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE); PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
final WakeLock wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "K9"); final WakeLock wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "K9");
wakeLock.setReferenceCounted(false); wakeLock.setReferenceCounted(false);
wakeLock.acquire(wakeLockTime); wakeLock.acquire(wakeLockTime);
if (K9.DEBUG)
Log.d(K9.LOG_TAG, "CoreService (" + getClass().getName() + ") queueing Runnable " + runner.hashCode() + " with startId " + startId);
Runnable myRunner = new Runnable() Runnable myRunner = new Runnable()
{ {
public void run() public void run()
@ -105,13 +118,13 @@ public abstract class CoreService extends Service
{ {
if (K9.DEBUG) if (K9.DEBUG)
Log.d(K9.LOG_TAG, "CoreService (" + getClass().getName() + ") running Runnable " + runner.hashCode() + " with startId " + startId); Log.d(K9.LOG_TAG, "CoreService (" + className + ") running Runnable " + runner.hashCode() + " with startId " + startId);
runner.run(); runner.run();
} }
finally finally
{ {
if (K9.DEBUG) if (K9.DEBUG)
Log.d(K9.LOG_TAG, "CoreService (" + getClass().getName() + ") completed Runnable " + runner.hashCode() + " with startId " + startId); Log.d(K9.LOG_TAG, "CoreService (" + className + ") completed Runnable " + runner.hashCode() + " with startId " + startId);
wakeLock.release(); wakeLock.release();
if (startId != null) if (startId != null)
{ {
@ -121,8 +134,20 @@ public abstract class CoreService extends Service
} }
}; };
if (threadPool == null)
threadPool.execute(myRunner); {
Log.e(K9.LOG_TAG, "CoreService.execute (" + className + ") called with no threadPool available; running Runnable " + runner.hashCode() + " in calling thread", new Throwable());
synchronized(this)
{
myRunner.run();
}
}
else
{
if (K9.DEBUG)
Log.d(K9.LOG_TAG, "CoreService (" + className + ") queueing Runnable " + runner.hashCode() + " with startId " + startId);
threadPool.execute(myRunner);
}
} }
public abstract void startService(Intent intent, int startId); public abstract void startService(Intent intent, int startId);
@ -138,7 +163,8 @@ public abstract class CoreService extends Service
public void onDestroy() public void onDestroy()
{ {
if (K9.DEBUG) if (K9.DEBUG)
Log.i(K9.LOG_TAG, "CoreService: " + this.getClass().getName() + ".onDestroy()"); Log.i(K9.LOG_TAG, "CoreService: " + className + ".onDestroy()");
threadPool.shutdown();
super.onDestroy(); super.onDestroy();
// MessagingController.getInstance(getApplication()).removeListener(mListener); // MessagingController.getInstance(getApplication()).removeListener(mListener);
} }