高性能线程池

两类线程池:存放着固定数量的线程,如果一些线程死亡时,线程池立马自动补齐,下面(fix);另一类存放在一定数量的线程,如果线程死亡需要手动补给,下面的(file);

入口:

package org.show.server;
import org.show.server.service.fix.FixThreadPoolService;
import org.show.server.service.life.LifeThreadPoolService;
public class StartUp {
    public static void main(String[] args) {
        // /// life
        LifeThreadPoolService poolService = LifeThreadPoolService.getInstance();
        poolService.startLiftSchedule();
        poolService.checkDeadScheduleThread();
        // //fix
        FixThreadPoolService fixThreadPoolService = FixThreadPoolService.getInstance();
        fixThreadPoolService.startFixScheduleThread();
    }
}


file:

package org.show.server.service.life;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class LifeThreadPoolService {
    private static final Logger logger = LoggerFactory.getLogger(LifeThreadPoolService.class);
    private static int threadAmount = 10;
    private static ThreadPoolExecutor threadPool = new ThreadPoolExecutor(threadAmount, threadAmount, 0L, TimeUnit.SECONDS, new SynchronousQueue<Runnable>(), new ThreadFactory() {
        private final AtomicInteger counter = new AtomicInteger();
        public Thread newThread(Runnable r) {
            Thread t = new Thread(r);
            t.setName("life Scheduler-" + counter.getAndIncrement());
            return t;
        }
    }, new RejectedExecutionHandler() {
        public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
            logger.warn("life schedule thread rejected.");
        }
    });
    private static final LifeThreadPoolService instance = new LifeThreadPoolService();
    private LifeThreadPoolService() {
    }
    public static LifeThreadPoolService getInstance() {
        return instance;
    }
    public void startLiftSchedule() {
        for (int i = 0; i < threadAmount; i++) {
            threadPool.submit(new LifeScheduleThread());
        }
    }
    public void checkDeadScheduleThread() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    try {
                        Thread.sleep(50);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    int aliveScheduleAmount = threadPool.getActiveCount();
                    for (; aliveScheduleAmount < threadAmount; aliveScheduleAmount++) {
                        threadPool.submit(new LifeScheduleThread());
                    }
                }
            }
        }).start();
    }
}


package org.show.server.service.life;
import java.util.Random;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class LifeScheduleThread implements Runnable {
    private static final Logger logger = LoggerFactory.getLogger(LifeScheduleThread.class);
    @Override
    public void run() {
        int time = new Random().nextInt(5000);
        logger.info("startSignal.countDown");
        try {
            Thread.sleep(time);
        } catch (InterruptedException e) {
        }
        logger.info("thread sleep time "+time);
    }
}


fix:

package org.show.server.service.fix;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;
public class FixThreadPoolService {
    private ExecutorService fixThreadPool = Executors.newFixedThreadPool(3, new ThreadFactory() {
        private AtomicInteger counter = new AtomicInteger();
        @Override
        public Thread newThread(Runnable r) {
            return new Thread(r, "fix Test-" + counter.incrementAndGet());
        }
    });
    private static final FixThreadPoolService instance = new FixThreadPoolService();
    private FixThreadPoolService() {
    }
    public static FixThreadPoolService getInstance() {
        return instance;
    }
    public void startFixScheduleThread() {
        for (int i = 0; i < 3; i++) {
            fixThreadPool.submit(new FixScheduleThread());
        }
    }
}
package org.show.server.service.fix;
import java.util.Random;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class FixScheduleThread implements Runnable {
    private static final Logger logger = LoggerFactory.getLogger(FixScheduleThread.class);
    @Override
    public void run() {
        int time = new Random().nextInt(5000);
        logger.info("startSignal.countDown");
        try {
            Thread.sleep(time);
        } catch (InterruptedException e) {
        }
        logger.info("thread sleep time " + time);
    }
}


供参考的。

高性能线程池,布布扣,bubuko.com

高性能线程池

上一篇:python下爬某个网页的图片


下一篇:javascript之getAttribute()方法 应用续