Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android ScheduledThreadPoolExecutor cause: null

I'm tearing my hair out with this one! I'm a newbie to Android so I'm sure it's something super obvious.

I'm getting a ScheduledThreadPoolExecutor exception where cause: null

All I want is a seperate thread that runs whenever the activity is on screen!

// Instance Variables
private ScheduledExecutorService m_oScheduledExecutor = null;

@Override
protected void onResume()
{
super.onResume();

if (oScheduledExecutor == null)
{
    oScheduledExecutor = Executors.newSingleThreadScheduledExecutor();
}

    try
    {
        oScheduledExecutor.scheduleAtFixedRate({Runnable Instance HERE}, 0, 10, TimeUnit.SECONDS);
    }
    catch (Exception e)
    {
        System.out.println("(MainActivity) Error: " + e.getMessage() + " Cause: " + e.getCause());
    }
}

@Override
protected void onStop()
{
    super.onStop();
    m_oScheduledExecutor.shutdown();
}

EDIT: Entire Stack Trace....

java.util.concurrent.RejectedExecutionException: Task java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask@41976688 rejected from java.util.concurrent.ScheduledThreadPoolExecutor@4195c7f8[Terminated, pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 1]
at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:1979)
at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:786)
at java.util.concurrent.ScheduledThreadPoolExecutor.delayedExecute(ScheduledThreadPoolExecutor.java:300)
at java.util.concurrent.ScheduledThreadPoolExecutor.scheduleAtFixedRate(ScheduledThreadPoolExecutor.java:545)
at java.util.concurrent.Executors$DelegatedScheduledExecutorService.scheduleAtFixedRate(Executors.java:619)
at com.example.wifitest.MainActivity.onResume(MainActivity.java:61)
at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1185)
at android.app.Activity.performResume(Activity.java:5182)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2732)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2771)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1276)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
like image 303
user2808151 Avatar asked Mar 23 '23 04:03

user2808151


1 Answers

You cannot 'recycle' an ExecutorService. Once you have invoked shutdown(), attempting to schedule any task will cause a rejection, and in your case the rejection policy is to throw RejectedExecutionException.

If you follow your stacktrace, you can see in ScheduledThreadPoolExecutor:

/**
 * Specialized variant of ThreadPoolExecutor.execute for delayed tasks.
 */
private void delayedExecute(Runnable command) {
    if (isShutdown()) {
        reject(command);
        return;
    }
    // ...
}

Keeping your executor service as an instance variable is not going to work for you here: once it's been shutdown, it can't be used again.

like image 170
wool.in.silver Avatar answered Apr 01 '23 16:04

wool.in.silver