java编程实战

Posted benming

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java编程实战相关的知识,希望对你有一定的参考价值。

线程池为什么要有它:

 

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

 

    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编程实战的主要内容,如果未能解决你的问题,请参考以下文章

Java并发编程实战 04死锁了怎么办?

Java并发编程实战基础概要

《Java8实战》读书笔记12:函数式编程

《Java8实战》读书笔记12:函数式编程

《java并发编程实战》

java并发编程实战学习--基础构建模块