RejectedExecutionException

RejectedExecutionException

我在这个文件中有java.util.concurrent.RejectedExecutionException。如我所见,在onStop调用之后,没有更多的进程在运行。不知道错误来自哪里。而且我确信执行程序不会执行更多的任务。

请帮助我找出错误的来源。

public static final String TAG = BroadcastService.class.getSimpleName();

private static final int TIMER_DELAY_SECONDS = 3;

private volatile JmDNS mService = null;
private WifiManager.MulticastLock mMulticastLock = null;
private ScheduledExecutorService mExecutorService = null;
private ScheduledFuture mPublisherFuture = null;
private ScheduledFuture mApiPublisherFuture = null;

private NetworkUtils mNetworkUtils = null;

private Runnable mDelayedKiller = null;

public static Intent getStartIntent(Context context) {
    final Intent serviceIntent = new Intent(context, BroadcastService.class);
    serviceIntent.setAction(BroadcastService.INTENT_ACTION_BROADCAST_START);
    return serviceIntent;
}

public static Intent getStopIntent(Context context) {
    final Intent serviceIntent = new Intent(context, BroadcastService.class);
    serviceIntent.setAction(BroadcastService.INTENT_ACTION_BROADCAST_STOP);
    return serviceIntent;
}

@Override
public void onCreate() {
    super.onCreate();
    mNetworkUtils = NetworkUtils.getInstance(getApplicationContext());
}

@Override
public int onStartCommand(final Intent intent, final int flags, final int startId) {
    if (intent == null) {
        return START_STICKY;
    }

    if (intent.getAction() != null) {
        switch (intent.getAction()) {
            case INTENT_ACTION_BROADCAST_START:
                startBroadcast();
                break;

            case INTENT_ACTION_BROADCAST_STOP:
                stopBroadcast();
                break;
        }
    }

    return START_STICKY;
}

@Nullable
@Override
public IBinder onBind(final Intent intent) {
    return null;
}

/**
 * Starts broadcast on a background thread
 */
public void startBroadcast() {
    if (mDelayedKiller != null) {
        NetworkThread.getCommonInstance().removeTask(mDelayedKiller);
        mDelayedKiller = null;
    }

    if (mExecutorService == null || mExecutorService.isShutdown()) {
        mExecutorService = Executors.newScheduledThreadPool(2);
    }

    if (mPublisherFuture != null) {
        mPublisherFuture.cancel(true);
    }

    final BonjourPublisher bonjourPublisher = new BonjourPublisher();
    mPublisherFuture = mExecutorService.schedule(bonjourPublisher, 2, TimeUnit.SECONDS);

    if (mApiPublisherFuture != null) {
        mApiPublisherFuture.cancel(true);
    }

    final ApiPublisher apiPublisher = new ApiPublisher();
    mApiPublisherFuture = mExecutorService.scheduleWithFixedDelay(apiPublisher, 0, 30, TimeUnit.SECONDS);

    //inform listeners
    EventBus.getDefault().post(new EventServiceBroadcasting(true));
}

public synchronized void stopBroadcast() {
    if (mPublisherFuture == null && mApiPublisherFuture == null) {
        return;
    }


    if (mPublisherFuture != null) {
        mPublisherFuture.cancel(true);
        if (mMulticastLock != null) {
            mMulticastLock.release();
            mMulticastLock = null;
        }
    }

    if (mApiPublisherFuture != null) {
        mApiPublisherFuture.cancel(true);
    }

    mDelayedKiller = new Runnable() {
        @Override
        public void run() {
            mExecutorService.shutdownNow();
            killService();
            stopSelf();
        }
    };

    NetworkThread.getCommonInstance().postDelayed(mDelayedKiller, 1000 * 20); //kill the service after 20 seconds

    //inform listeners
    EventBus.getDefault().post(new EventServiceBroadcasting(false));
}

@Override
public void onDestroy() {
    super.onDestroy();
    killService();
}

private synchronized void killService() {
    if (mService != null) {
        try {
            mService.unregisterAllServices();
            mService.close();
            mService = null;
        } catch (IOException e) {
            e.printStackTrace();
        }
    } else {
    }
}

public static class DiscoverableAssistant {

    private DiscoverableAssistant() {
    }

    public static boolean isDiscoverable(Context context) {
        final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
        return prefs.getBoolean(PREF_DEVICE_DISCOVERABLE, true); //true by default
    }

    public static void setDiscoverable(Context context, boolean discoverable) {
        final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
        prefs.edit().putBoolean(PREF_DEVICE_DISCOVERABLE, discoverable).apply();
    }
}

private class BonjourPublisher implements Runnable {

    @Override
    public void run() {

        final String serviceName = mNetworkUtils.getDeviceName(BroadcastService.this);
        final String serviceType = getString(R.string.multi_dns_network_name);

        final Map<String, String> properties = new HashMap<>();
        properties.put(DeviceViewActivity.DEVICE_PROPERTY_DEVICE_TYPE, "Android");
        properties.put(DeviceViewActivity.DEVICE_PROPERTY_FILE_SERVER_PORT,
                String.valueOf(mNetworkUtils.getAssignedPort()));
        if (DiscoverableAssistant.isDiscoverable(BroadcastService.this)) {
            properties.put(DeviceViewActivity.DEVICE_PROPERTY_DISCOVERABLE, "true");
        } else {
            properties.put(DeviceViewActivity.DEVICE_PROPERTY_DISCOVERABLE, "false");
        }

        //acquire wifi multicast lock
        if (mMulticastLock == null) {
            final WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
            mMulticastLock.setReferenceCounted(true);
            mMulticastLock.acquire();
        }

        try {
            if (mService == null) {
                mService = JmDNS.create(mNetworkUtils.getMyInet4Address(),
                        NetworkUtils.getHostName(mNetworkUtils.getDeviceName(BroadcastService.this)));
            }

            final ServiceInfo info = ServiceInfo.create(serviceType, serviceName, mNetworkUtils.getAssignedPort(), 0, 0, true, properties);
            while (mService != null) {
                mService.registerService(info);
                Thread.sleep(TIMER_DELAY_SECONDS * 1000);
                mService.unregisterAllServices();
                Thread.sleep(1000);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
        } catch (Exception e) {
        }
    }
}

private class ApiPublisher implements Runnable {

    private APo api = null;
    private SimplifiedDeviceInfo mDeviceInfo = null;

    public ApiPublisher() {
        api = Utils.getRetrofitInstance(BroadcastService.this, null)
                .create(api.class);
    }

    @Override
    public void run() {
        try {
            if (mDeviceInfo == null) {
                mDeviceInfo = new SimplifiedDeviceInfo(mNetworkUtils.getDeviceName(BroadcastService.this),
                        mNetworkUtils.getMyInet4Address().getHostAddress(), mNetworkUtils.getAssignedPort(),
                        NetworkUtils.getDeviceType(), BroadcastService.DiscoverableAssistant.isDiscoverable(BroadcastService.this));
            }

            Call<JsonElement> call = api.broadcastDevice(mDeviceInfo);
            call.execute();
        } catch (Exception e) {
        }
    }
}

最佳答案

当您尝试将任务提交给执行者时,将抛出RejectedExecutionException,并且将其拒绝。在这种情况下,异常消息中会有一个线索:

java.util.concurrent.ScheduledThreadPoolExecutor@42209b70[
    Sh‌​‌​utting down, pool size = 2, active threads = 2,
    queued tasks = 0, completed tasks = 248]


这告诉我您正在尝试向正在关闭的Executor提交任务。

现在,我不假装理解您的代码实际上在做什么,但是我可以看到您正在使用postThread安排一个Runnable来关闭执行程序。我的猜测是这样完成的……然后您尝试提交另一个任务。

在阅读您的代码时,我发现了几个可以抓住的地方,然后压下Exception。那真是个坏主意。

如果这就是为什么您在调试代码时遇到麻烦,我不会感到惊讶。

10-05 17:55