Custom thread pool briefly

       Related concepts thread pool is not here explained, Baidu, there are many, here a brief presentation about how the thread pool a custom line thread management, if we are to achieve a thread pool management thread, then the need to achieve the following points ideas:

1. How to manage threads

2. How to define how the worker and the worker continued to keep running

3. How to define a thread pool size and the size of the queue

4. How to provide an interface to the caller using

5. How close the thread pool thread

Then we realize every one of these issues.

1. We need to define a queue to manage threads, used here LinkedBlockingQueue

// 1. Define a storage queue thread 
private LinkedBlockingQueue <Runnable> queue;

 2. Because it is a simple test, so we can define a class to implement internal worker threads

// Define the working thread execution threads 
    class {the Worker the extends the Thread 

        Private SelfThreadPoolExecutor ThreadPoolExecutor; 

        public the Worker (SelfThreadPoolExecutor poolExecutor) { 
            this.threadPoolExecutor = poolExecutor; 
        } 

        @Override 
        public void RUN () { 
            the Runnable Task; 
            the while (threadPoolExecutor.receiveTask threadPoolExecutor.queue.size || ()> 0) { 
                the try { 
                    // thread have then taken out, or wait 
                    System.out.println ( "ready to consume thread"); 
                    Task = threadPoolExecutor.queue.take (); 
                    IF ( task! = null) { 
                        task.run ();
                        System.out.println ( "thread consumption"); 
                    } 
                } the catch (InterruptedException E) { 
                    e.printStackTrace (); 
                } 
            } 
        } 
    }

 SelfThreadPoolExecutor is externally defined the whole class name

3. Use the constructor has parameters to manage the thread pool size

// store the working set of threads 3. 
    Private List <the Worker> workerList; 

    // initialize the thread pool 4. 
    public SelfThreadPoolExecutor (coresize The int, int QueueSize) { 
        IF (coresize The <QueueSize = 0 || <= 0) { 
            the throw an IllegalArgumentException new new ( "incorrect parameter"); 
        } 
        this.queue a LinkedBlockingQueue new new = <> (QueueSize); 
        // set of thread-safe 
        this.workerList the Collections.synchronizedList = (new new the ArrayList <> ()); 
        for (int I = 0; I <coresize The; I ++) { 
            the worker new new worker = the worker (the this); 
            worker.start (); 
            workerList.add (worker); 
        } 
    }

4. Define blocking and nonblocking corresponding interface provided by way of

5. The method of interfaces nonblocking // 
    public Boolean the offer (the Runnable Task) { 
        IF (receiveTask) { 
            return Queue.offer (Task); 
        } the else { 
            return to false; 
        } 
    } 

    // 6. The method of blocking interface 
    public void put ( Task the Runnable) { 
        the try { 
            IF (receiveTask) { 
                queue.put (Task); 
            } 
        } the catch (InterruptedException E) { 
            e.printStackTrace (); 
        } 
    }

6. close the thread pool

// Close the thread pool 7. 
    Private receiveTask = Boolean to true; 

    public void the shutdown () { 
        // 7.1 queue is no longer receive thread. 
        ReceiveTask = to false; 
        // wait or block 7.2 is closed thread. 
        For (the Thread Thread: workerList ) { 
            IF (Thread.State.BLOCKED.equals (thread.getState ()) 
            || Thread.State.WAITING.equals (thread.getState ()) 
            || Thread.State.TIMED_WAITING.equals (thread.getState ()) ) { 
                Thread.interrupt (); 
            } 
        } 
    }

 We tested the following method:

public static void main(String [] args){
        SelfThreadPoolExecutor selfThreadPoolExecutor = new SelfThreadPoolExecutor(5,10);
        for(int i = 0;i < 20;i++){
            Runnable task = () ->{
                System.out.println("开启线程");
            };
            selfThreadPoolExecutor.put(task);
        }
        selfThreadPoolExecutor.shutdown();
    }

 Operating results are:

Ready to consume thread 
ready to consume thread 
ready to consume thread 
ready to consume thread 
ready to consume thread 
open thread 
consumption threads 
ready to consume thread 
open thread 
consumption threads 
ready to consume thread 
open thread 
consumption threads 
ready to consume thread 
. . . . . .

 Overall codes in the Annex.

 

Guess you like

Origin www.iteye.com/blog/357029540-2443613