java programming combat

Why did it have a thread pool:

 

    Create a virtual machine to open up a thread stack, release threads to garbage collection.

 

    server -side concurrent access to the database.

 

    There stood the server starts the thread pool.

 

 -----

 

    Thread pool concept:

 

            1. Task Queue

 

            2. rejection policy ( an exception is thrown, discarded directly, obstruction, in a temporary queue )

 

            3. The initial values init (min), the initial thread pool size

 

            3.active

 

            5.max the maximum number of threads in the thread pool

 

            min<=active<=max

 

 quartz: regular tasks

Code:

package chapter13;

 

import java.util.ArrayList;

java.util.LinkedList import;

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) {// other methods have read operation, write operation here, so you have to lock the

            TASK_QUEUE.addLast(runnable);

            TASK_QUEUE.notifyAll();

        }

    }

 

    /**

     * Thread in ThreadPool

     */

    private static class WorkerTask extends Thread {// Why are defined as private do not want is to let people know what I

        private volatile TaskState taskState = TaskState.FREE;

 

        public WorkerTask (ThreadGroup group, String name ) {// constructor calls Thread configuration

            super(group, name);

        }

 

        public TaskState getTaskState() {

            return this.taskState;

        }

 

        public void close() {

            this.taskState = TaskState.DEAD;

        }

 

        public void run () {// rewrite forex rebate guarantee can not hang up after executing

            OUTER:

            while (this.taskState! = TaskState.DEAD) { // current thread is not dead

                Runnable runnable;

                synchronized (TASK_QUEUE) {// go task queue retrieval task

                    while (TASK_QUEUE.isEmpty()) {

                        try {

                            taskState = TaskState.BLOCKEED;

                            TASK_QUEUE.wait (); // no task lock is released, this thread TASK_QUEUE waiting on the wait will be interrupted interrupted, then to break out

                        } catch (InterruptedException e) {

                            break OUTER; // https: //blog.csdn.net/zhaoheng2017/article/details/78385973   was interrupted to the transfer of the task to then

                        }

                    }

                    runnable = TASK_QUEUE.removeFirst (); // queue FIFO queue tasks such as database only be consumed once the thread pool, take out my connection to perform on it

                }

                if (runnable != null) {

                    taskState = TaskState.RUNNING;

                    runnable.run();

                    taskState = TaskState.FREE;

                }

            }

        }

}

 

 }

Original link: https: //blog.csdn.net/qq_28764557/article/details/103575987

Guess you like

Origin www.cnblogs.com/benming/p/12066934.html