当前位置:网站首页>Creation and use of thread pool

Creation and use of thread pool

2022-07-07 05:14:00 Yang Asang 815

public ThreadPoolExecutor(int corePoolSize, Number of core threads 
                      int maximumPoolSize,  The maximum number of threads supported by this thread pool 
                      long keepAliveTime,  Specify the time for the thread to survive at zero 
                      TimeUnit unit,  Specify the unit of survival time   second   branch   when   God 
                      BlockingQueue<Runnable> workQueue,  Specify task queue 
                      ThreadFactory threadFactory,  Specify which thread factory to create the thread 
                      RejectedExecutionHandler handler)   The specified thread is busy   When the task is full , What about the new task ?

import java.util.concurrent.*;

public class TestDamo {
    
    public static void main(String[] args) {
        /*
        * public ThreadPoolExecutor(int corePoolSize, Number of core threads 
                              int maximumPoolSize,  The maximum number of threads supported by this thread pool 
                              long keepAliveTime,  Specify the time for the thread to survive at zero 
                              TimeUnit unit,  Specify the unit of survival time   second   branch   when   God 
                              BlockingQueue<Runnable> workQueue,  Specify task queue 
                              ThreadFactory threadFactory,  Specify which thread factory to create the thread 
                              RejectedExecutionHandler handler)   The specified thread is busy   When the task is full , What about the new task ?
        * */
        ExecutorService pool=
                new ThreadPoolExecutor
                        (3,5,8,
                                TimeUnit.SECONDS,new ArrayBlockingQueue<>(6),
                                Executors.defaultThreadFactory(),
                                new ThreadPoolExecutor.AbortPolicy());
        MyRunable myRunable=new MyRunable();
        pool.execute(myRunable);// perform Runable
        /*
        * Future<T>submit (Callable<T>task)  perform Callable
        * void shutdown() Wait for the thread to close after the task is executed 
        * List<Runable>shutdownNow()  Close thread now , Stop the method being executed , And return the unimplemented task 
        * */
        pool.shutdown();
    }
}
class MyRunable implements Runnable{

    @Override
    public void run() {
        try {
            for (int i = 0; i < 5; i++) {
                Thread.sleep(2000);
                System.out.println(" Thread running :"+(i+1));
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

原网站

版权声明
本文为[Yang Asang 815]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207062309112264.html