Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RxJava线程 #43

Open
zengjingfang opened this issue Jul 5, 2018 · 1 comment
Open

RxJava线程 #43

zengjingfang opened this issue Jul 5, 2018 · 1 comment
Assignees
Labels

Comments

@zengjingfang
Copy link
Owner

No description provided.

@zengjingfang zengjingfang self-assigned this Jul 5, 2018
@zengjingfang
Copy link
Owner Author

computationScheduler >>> EventLoopsScheduler

EventLoopsScheduler.java

static final FixedSchedulerPool NONE = new FixedSchedulerPool(null, 0);

static final class FixedSchedulerPool {
        final int cores;

        final PoolWorker[] eventLoops;
        long n;

        FixedSchedulerPool(ThreadFactory threadFactory, int maxThreads) {
            // initialize event loops
            this.cores = maxThreads;
            this.eventLoops = new PoolWorker[maxThreads];  // 这里新建了一个线程组
            for (int i = 0; i < maxThreads; i++) {
                // new出每个线程
                this.eventLoops[i] = new PoolWorker(threadFactory);
            }
        }

        public PoolWorker getEventLoop() {
            int c = cores;
            if (c == 0) {
                return SHUTDOWN_WORKER;
            }
            // simple round robin, improvements to come
            return eventLoops[(int)(n++ % c)];
        }

        public void shutdown() {
            for (PoolWorker w : eventLoops) {
                w.unsubscribe();
            }
        }
    }

    static final class PoolWorker extends NewThreadWorker {
        PoolWorker(ThreadFactory threadFactory) {
            super(threadFactory);
        }
    }

NewThreadWorker.java

    public NewThreadWorker(ThreadFactory threadFactory) {
        ScheduledExecutorService exec = Executors.newScheduledThreadPool(1, threadFactory);
        // Java 7+: cancelled future tasks can be removed from the executor thus avoiding memory leak
        boolean cancelSupported = tryEnableCancelPolicy(exec);
        if (!cancelSupported && exec instanceof ScheduledThreadPoolExecutor) {
            registerExecutor((ScheduledThreadPoolExecutor)exec);
        }
        executor = exec;
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Android开发历险记
  
Awaiting triage
Development

No branches or pull requests

1 participant