码迷,mamicode.com
首页 > 其他好文 > 详细

SimpleThreadPool极简版

时间:2019-12-23 19:23:10      阅读:82      评论:0      收藏:0      [点我收藏+]

标签:ISE   fir   拒绝   sync   get   inter   lock   code   while   

package com.dwz.concurrency.chapter13;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * 1.任务队列--调度 2.拒绝策略(抛出异常、直接丢弃、阻塞、临时队列)--性能保护 3.init(min) 4.active 5.max
 * min<=active<=max Quartz/Control-M
 */
public class SimpleThreadPool {
    private final int size;
    private final static int DEFAULT_SIZE = 10;
    private static volatile int seq = 0;
    private final static String THREAD_PREFIX = "SIMPLE_THREAD_POOL-";
    private final static ThreadGroup GROUP = new ThreadGroup("Pool_Group");
    private final static LinkedList<Runnable> TASK_QUEUE = new LinkedList<>();
    private final static List<WorkerTask> THREAD_QUEUE = new ArrayList<>();

    public SimpleThreadPool() {
        this(DEFAULT_SIZE);
    }

    public SimpleThreadPool(int size) {
        this.size = size;
        init();
    }

    private void init() {
        for (int i = 0; i < this.size; i++) {
            createWorkTask();
        }
    }

    public void submit(Runnable runnable) {
        synchronized (TASK_QUEUE) {
            TASK_QUEUE.addLast(runnable);
            TASK_QUEUE.notifyAll();
        }
    }

    private void createWorkTask() {
        WorkerTask task = new WorkerTask(GROUP, THREAD_PREFIX + (seq++));
        task.start();
        THREAD_QUEUE.add(task);
    }

    private enum TaskState {
        FREE, RUNNING, BLOCKED, DEAD
    }

    private static class WorkerTask extends Thread {
        private volatile TaskState taskState = TaskState.FREE;

        public WorkerTask(ThreadGroup group, String name) {
            super(group, name);
        }

        public TaskState getTaskState() {
            return this.taskState;
        }

        @Override
        public void run() {
            OUTER: 
            while (this.taskState != TaskState.DEAD) {
                Runnable runnable = null;
                synchronized (TASK_QUEUE) {
                    while (TASK_QUEUE.isEmpty()) {
                        try {
                            this.taskState = TaskState.BLOCKED;
                            TASK_QUEUE.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                            // 线程被打断回到OUTER位置
                            break OUTER;
                        }
                    }
                    runnable = TASK_QUEUE.removeFirst();
                }
                
                if (runnable != null) {
                    System.out.println("runnable into...");
                    this.taskState = TaskState.RUNNING;
                    runnable.run();
                    this.taskState = TaskState.FREE;
                }
            }
        }

        public void close() {
            this.taskState = TaskState.DEAD;
        }
    }

    public static void main(String[] args) {
        SimpleThreadPool threadPool = new SimpleThreadPool();
//        IntStream.range(0, 40).forEach(i -> {
//            threadPool.submit(() -> {
//                System.out.println("The runnable " + i + " be serviced by " + Thread.currentThread() + " start.");
//                try {
//                    Thread.sleep(1000);
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
//                System.out.println("The runnable " + i + " be serviced by " + Thread.currentThread() + " finished.");
//            });
//        });
        for(int i = 0; i < 40; i++) {
            threadPool.submit(() -> {
                System.out.println("The runnable be serviced by " + Thread.currentThread() + " start.");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("The runnable be serviced by " + Thread.currentThread() + " finished.");
            });
        }
    }
}

 

SimpleThreadPool极简版

标签:ISE   fir   拒绝   sync   get   inter   lock   code   while   

原文地址:https://www.cnblogs.com/zheaven/p/12085063.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!