美文网首页
java-threadpool

java-threadpool

作者: jianshuqiang | 来源:发表于2019-07-12 15:28 被阅读0次

    线程池的好处

    利用线程池管理多线程可以控制最大并发的问题,同时可以只能加服务器资源的利用率

    线程池的简单使用

    1,线程池管理类

    package com.learn.test.demo.ThreadPoolExample;
    
    import javax.annotation.security.RunAs;
    import java.util.LinkedList;
    import java.util.Queue;
    import java.util.concurrent.*;
    
    public class ThreadPoolManager {
        private static ThreadPoolManager tpm = new ThreadPoolManager();
        //可执行线程数为MAX_POOL_SIZE+WORK_QUEUE_SIZE=320
        // 线程池维护线程的最少数量
        private final static int CORE_POOL_SIZE = 33;
        // 线程池维护线程的最大数量
        private final static int MAX_POOL_SIZE = 300;
        // 线程池维护线程所允许的空闲时间
        private final static int KEEP_ALIVE_TIME = 100;
        // 线程池所使用的缓冲队列大小
        private final static int WORK_QUEUE_SIZE = 20;
        // 消息缓冲队列,当当前线程数>(最大线程数+队列中线程数)的时候将加入到这个队列中
        Queue<ThreadJob> msgQueue = new LinkedList<ThreadJob>();
    
        public static ThreadPoolManager newInstance() {
            return tpm;
        }
    
        //判断任务队列是否为空
        private boolean hasMoreAcquire() {
            return !msgQueue.isEmpty();
        }
    
        //将对于的线程放到另外一个队列中
        final RejectedExecutionHandler handler = new RejectedExecutionHandler() {
            // 进行重新入队
            @Override
            public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
                System.out.println(((ThreadJob) r).getThreaeName() + "消息放入队列中重新等待执行");
                msgQueue.offer(((ThreadJob) r));
            }
        };
        // 线程池
        final ThreadPoolExecutor threadPool = new ThreadPoolExecutor(
                CORE_POOL_SIZE, MAX_POOL_SIZE, KEEP_ALIVE_TIME,
                TimeUnit.MILLISECONDS, new ArrayBlockingQueue(WORK_QUEUE_SIZE),
                this.handler);
        //可以用来处理多余线程
        final Runnable runnable = new Runnable() {
            @Override
            public void run() {
                System.out.println("管理线程池中线程启动");
                if (hasMoreAcquire()) {
                    System.out.println("开始分析分析队列中的数据");
                    ThreadJob threadJob = msgQueue.poll();
                    System.out.println(threadJob.getThreaeName());
                }
    
            }
        };
    
        public void addThread(String threadName) {
            Runnable task;
            try {
                task = new ThreadJob(threadName);
                threadPool.execute(task);
            } catch (Exception e) {
    
    
            }
        }
    
        // 管理数据库访问的线程池
    
    
        // 调度线程池
        final ScheduledExecutorService scheduler = Executors
                .newScheduledThreadPool(1);
        //从现在开始每350毫秒执行一次,执行的是奔类中的run方法
        final ScheduledFuture taskHandler = scheduler.scheduleAtFixedRate(
                runnable, 0, 350, TimeUnit.MILLISECONDS);
    }
    
    

    2,线程类

    package com.learn.test.demo.ThreadPoolExample;
    
    public class ThreadJob implements Runnable {
        private String threaeName;
    
        public ThreadJob(String threaeName) {
            this.threaeName = threaeName;
        }
    
        public String getThreaeName() {
            return threaeName;
        }
    
        public void setThreaeName(String threaeName) {
            this.threaeName = threaeName;
        }
    
        @Override
        public void run() {
            System.out.println("开始执行 ThreadJob--> run");
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(this.threaeName);
    
        }
    }
    
    

    3,测试类

    package com.learn.test.demo.ThreadPoolExample;
    
    public class TestThreadPool {
        public static void main(String[] args) {
            ThreadPoolManager threadPoolManager=ThreadPoolManager.newInstance();
            for (int i = 0; i < 1000; i++) {
                threadPoolManager.addThread("hello threadpool-->"+i);
                System.out.println("线程队列中的数据:"+  threadPoolManager.msgQueue.size());
            }
    
    
        }
    }
    
    

    相关文章

      网友评论

          本文标题:java-threadpool

          本文链接:https://www.haomeiwen.com/subject/rvzekctx.html