码迷,mamicode.com
首页 > 编程语言 > 详细

java编程实战

时间:2019-12-19 12:56:46      阅读:67      评论:0      收藏:0      [点我收藏+]

标签:exception   wait   syn   取出   move   public   extend   worker   turn   

线程池为什么要有它:

 

    线程创建要开辟虚拟机栈,释放线程要垃圾回收的。

 

    server端要并发访问数据库的。

 

    服务器启动有线程池放着。

 

 -----

 

    线程池的概念:

 

            1.任务队列

 

            2.拒绝策略(抛出异常,直接丢弃,阻塞,放在临时队列)

 

            3.初始化值init(min),初始线程池大小

 

            3.active

 

            5.max线程池线程的最大个数

 

            min<=active<=max

 

 quartz:定时任务

代码:

package chapter13;

 

import java.util.ArrayList;

import java.util.LinkedList;

import java.util.List;

 

public class SimpleThreadPoolMy {

 

    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 List<WorkerTask> THREAD_QUEUE = new ArrayList<>();

 

    /**

     * task queue

     */

    private final static LinkedList<Runnable> TASK_QUEUE = new LinkedList<>();

 

    public SimpleThreadPoolMy() {

        this(DEFAULT_SIZE);

    }

 

    public SimpleThreadPoolMy(int size) {

        this.size = size;

        init();

    }

 

    private void init() {

        for (int i = 0; i < size; i++) {

            createWorkTask();

        }

    }

 

    private void createWorkTask() {

        WorkerTask task = new WorkerTask(GROUP, THREAD_PREFIX + (seq++));

        task.start();

        THREAD_QUEUE.add(task);

    }

 

    private enum TaskState {

        FREE, RUNNING, BLOCKEED, DEAD

    }

 

    public void submit(Runnable runnable) {

        synchronized (TASK_QUEUE) {//别的方法有读的操作,这里有写的操作,所以你要加锁的

            TASK_QUEUE.addLast(runnable);

            TASK_QUEUE.notifyAll();

        }

    }

 

    /**

     * Thread in ThreadPool

     */

    private static class WorkerTask extends Thread {//为什么定义为private呢就是为了不想让别人知道我是什么

        private volatile TaskState taskState = TaskState.FREE;

 

        public WorkerTask(ThreadGroup group, String name) {//构造方法,调用了Thread的构造

            super(group, name);

        }

 

        public TaskState getTaskState() {

            return this.taskState;

        }

 

        public void close() {

            this.taskState = TaskState.DEAD;

        }

 

        public void run() {//重写外汇返佣保证执行完之后不能挂掉

            OUTER:

            while (this.taskState != TaskState.DEAD) {//当前的线程没有死掉

                Runnable runnable;

                synchronized (TASK_QUEUE) {//去任务队列调取任务

                    while (TASK_QUEUE.isEmpty()) {

                        try {

                            taskState = TaskState.BLOCKEED;

                            TASK_QUEUE.wait();//没有任务就释放锁,这个线程在TASK_QUEUE上等待 wait会被打断  打断的话就break出来

                        } catch (InterruptedException e) {

                            break OUTER;//https://blog.csdn.net/zhaoheng2017/article/details/78385973  被打断了就接着去调取任务

                        }

                    }

                    runnable = TASK_QUEUE.removeFirst();//队列 先进先出 任务队列只被消费一次 比如数据库线程池,取出来我执行连接就可以了

                }

                if (runnable != null) {

                    taskState = TaskState.RUNNING;

                    runnable.run();

                    taskState = TaskState.FREE;

                }

            }

        }

}

 

 }

原文链接:https://blog.csdn.net/qq_28764557/article/details/103575987

java编程实战

标签:exception   wait   syn   取出   move   public   extend   worker   turn   

原文地址:https://www.cnblogs.com/benming/p/12066934.html

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