多线程

作者: yinhaijun | 来源:发表于2019-01-22 11:04 被阅读9次

    线程基本介绍和创建方式

    参考Java 多线程编程

    jdk自带的线程池

    java.util.concurrent包提供了线程相关的类,ThreadPoolExecutor用于创建一个线程池,通常我们会这样创建一个线程池

    private ThreadPoolExecutor scheduleExecutor =
                new ThreadPoolExecutor(100, 100, 0, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<>(100),
                        new NamedThreadFactory("basic-data"), new LoggerDiscardPolicy());
    

    spring的线程池

    spring的线程池是对ThreadPoolExecutor的封装

    @Configuration
    public class ThreadPoolConfig {
        @Bean("threadPoolTaskExecutor")
        public ThreadPoolTaskExecutor threadPoolTaskExecutor(){
            ThreadPoolTaskExecutor threadPoolTaskExecutor=new ThreadPoolTaskExecutor();
            threadPoolTaskExecutor.setCorePoolSize(4);
            threadPoolTaskExecutor.setKeepAliveSeconds(10);
            threadPoolTaskExecutor.setMaxPoolSize(10);
            threadPoolTaskExecutor.setQueueCapacity(10);
            return threadPoolTaskExecutor;
    
        }
    }
    

    相关文章

      网友评论

          本文标题:多线程

          本文链接:https://www.haomeiwen.com/subject/pchljqtx.html