io.dyn.core.Tasks.java Source code

Java tutorial

Introduction

Here is the source code for io.dyn.core.Tasks.java

Source

/*
 * Copyright (c) 2011-2012 by the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.dyn.core;

import java.util.Random;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

import io.dyn.core.sys.Sys;
import org.springframework.core.task.TaskExecutor;

/**
 * @author Jon Brisbin <jon@jbrisbin.com>
 */
public abstract class Tasks {

    private Tasks() {
    }

    private static int corePoolSize = Integer
            .parseInt(System.getProperty("io.dyn.tasks.CorePoolSize", "" + Sys.PROCESSORS));
    private static int workerPoolSize = Integer
            .parseInt(System.getProperty("io.dyn.tasks.WorkerPoolSize", "" + Sys.PROCESSORS * 2));

    private static final Random random = new Random();

    private static final ThreadLocal<TaskExecutor> CURRENT_EXECUTOR = new ThreadLocal<>();

    public static final TaskExecutor[] CORE_EXECUTORS = new TaskExecutor[corePoolSize];
    public static final TaskExecutor[] WORKER_EXECUTORS = new TaskExecutor[workerPoolSize];
    public static final TaskExecutor LOG_EXECUTOR = new NonBlockingTaskExecutor("log");

    static {
        fillPool("core", CORE_EXECUTORS, corePoolSize);
        fillPool("worker", WORKER_EXECUTORS, workerPoolSize);
    }

    public static TaskExecutor currentExecutor() {
        return CURRENT_EXECUTOR.get();
    }

    public static void currentExecutor(TaskExecutor te) {
        CURRENT_EXECUTOR.set(te);
    }

    public static void execute(Runnable r, TaskExecutor te) {
        if (null != te && te != CURRENT_EXECUTOR.get()) {
            te.execute(r);
        } else {
            r.run();
        }
    }

    public static TaskExecutor randomCoreExecutor() {
        return randomExecutor(CORE_EXECUTORS);
    }

    public static TaskExecutor randomWorkerExecutor() {
        return randomExecutor(WORKER_EXECUTORS);
    }

    public static TaskExecutor randomExecutor(TaskExecutor[] executors) {
        TaskExecutor te = executors[random.nextInt(executors.length)];
        if (te != CURRENT_EXECUTOR.get()) {
            return te;
        } else if (executors.length > 1) {
            return randomExecutor(executors);
        } else {
            return null;
        }
    }

    public static ThreadFactory newThreadFactory(String name) {
        return new DynIoThreadFactory(name);
    }

    private static void fillPool(String baseName, TaskExecutor[] pool, int size) {
        for (int i = 0; i < size; i++) {
            pool[i] = new NonBlockingTaskExecutor(baseName);
        }
    }

    private static class DynIoThreadFactory implements ThreadFactory {
        private static AtomicInteger counter = new AtomicInteger(1);

        private String prefix;

        DynIoThreadFactory(String prefix) {
            this.prefix = prefix;
        }

        @Override
        public Thread newThread(Runnable r) {
            Thread t = new Thread(r);
            t.setDaemon(false);
            t.setPriority(Thread.NORM_PRIORITY);
            t.setName("dyn.io-" + prefix + "-" + counter.getAndIncrement());
            return t;
        }
    }

}