Skip to content

Use shared threadpool #120

@jackpf

Description

@jackpf

e.g.

public class AppExecutors {
    private static final AppExecutors INSTANCE = new AppExecutors();

    private final ExecutorService background;
    private final Executor mainThread;

    private AppExecutors() {
        this.background = Executors.newCachedThreadPool();
        this.mainThread = new MainThreadExecutor();
    }

    public static AppExecutors getInstance() {
        return INSTANCE;
    }

    public ExecutorService background() {
        return background;
    }

    public Executor mainThread() {
        return mainThread;
    }

    private static class MainThreadExecutor implements Executor {
        private final Handler mainThreadHandler = new Handler(Looper.getMainLooper());

        @Override
        public void execute(Runnable command) {
            mainThreadHandler.post(command);
        }
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions