简体   繁体   English

ScheduleAtFixedRate执行一次(或等于corePoolSize相等的次数)

[英]ScheduleAtFixedRate executes once (or as many times as corePoolSize equals)

I have created MyThreadPoolExecutor 我已经创建了MyThreadPoolExecutor

public class MyThreadPoolExecutor extends ScheduledThreadPoolExecutor {
    private final Context ctx;

    public MyThreadPoolExecutor(int corePoolSize, Context ctx, String threadNamePrefix)
    {
        super(corePoolSize);
        MyThreadFactory factory = new MyThreadFactory(new ThreadExceptionHandler(ctx), threadNamePrefix);
        setThreadFactory(factory);
        this.ctx = ctx;
    }

    @Override
    public void afterExecute(Runnable r, Throwable t) {
        super.afterExecute(r, t);
        LogUtils.i(Tags.THREAD_EXECUTED, Thread.currentThread().getName());

        if (t == null && r instanceof Future<?>) {
            try {
                Object result = ((Future<?>) r).get();
            } catch (CancellationException e) {
                t = e;
            } catch (ExecutionException e) {
                t = e.getCause();
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        }

        if (t != null) {
            LogUtils.e(Tags.UNCAUGHT_EXCEPTION, "Uncaught exception error");
            Utils.handleUncaughtException(ctx, t);
        }
    }
}

I have MyThreadFactory 我有MyThreadFactory

public class MyThreadFactory implements ThreadFactory {
private static final ThreadFactory defaultFactory = Executors.defaultThreadFactory();
private final Thread.UncaughtExceptionHandler handler;
private final String threadName;

public MyThreadFactory(Thread.UncaughtExceptionHandler handler, @NonNull String threadName) {
    this.handler = handler;
    this.threadName = threadName;
}

@Override
public Thread newThread(@NonNull Runnable runnable) {
    Thread thread = defaultFactory.newThread(runnable);
    thread.setUncaughtExceptionHandler(handler);
    if (threadName != null) {
        thread.setName(threadName +"-" + thread.getId());
    }
    LogUtils.i(Tags.THREAD_CREATED, thread.getName());
    return thread;
}

} }

I need to execute thread every 1 second to do some stuff. 我需要每1秒执行一次线程来做一些事情。 When I use my own MyScheduledThreadPoolExecutor it runs only as many times as corePoolSize equals. 当我使用自己的MyScheduledThreadPoolExecutor时,它的运行次数只有corePoolSize的相等。 So when corePoolSize equals 3, my thread runs 3 times. 因此,当corePoolSize等于3时,我的线程运行3次。

When I use ScheduledThreadPoolExecutor the above problem does not exist. 当我使用ScheduledThreadPoolExecutor时,上述问题不存在。

Below the way I run it: 下面,我运行它:

commander = new MyThreadPoolExecutor(corePoolSize, getApplicationContext(), threadNamePrefix);
commander.scheduleAtFixedRate(new Runnable() {
        @Override
        public void run() {
            LogUtils.i(Tags.DISPLAY_ACTIVITY, "Check display " + Thread.currentThread().getName());
        }
    }, 1000, PERIOD_CHECK_TIME_FRAME, TimeUnit.MILLISECONDS);

What am I missing? 我想念什么? What am I doing wrong? 我究竟做错了什么?

ScheduledExecutorService blocks subsequent invocation if a runtime exception occurs while executing the runnable without throwing the exception (Really sad). 如果在运行可运行程序时发生运行时异常而没有引发异常,则ScheduledExecutorService阻止后续调用(真的很难过)。

This might be your issue. 这可能是您的问题。 Probably some where the code is throwing exception and therefore no more invocations ever happen. 可能是代码抛出异常的地方,因此不再发生任何调用。 Try wrapping the runnable inside a try catch to see if there is any exception thrown. 尝试将可运行对象包装在try catch中,以查看是否抛出任何异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM