• Day_19多线程(下)


    Day_19多线程(下)

    线程池

    并发队列

    分为两种:阻塞队列,非阻塞队列

    假设队列容器长度为10.

    入队

    非阻塞队列:当队列中满了的时候,放入数据,数据丢失。

    阻塞队列:当队列满了的时候,进行等待,什么时候队列中有出队的数据,那么第11个再放进去。

    出队

    非阻塞队列:如果现在队列中没有元素,去数据,得到的是null。

    阻塞队列:等待,什么时候放进去了,再取出来。

    非阻塞队列

    import java.util.concurrent.ConcurrentLinkedQueue;
    
    public class duilieTest {
        public static void main(String[] args) {
            //无边界队列:没有长度限制
            ConcurrentLinkedQueue<String> clq=new ConcurrentLinkedQueue<String>();
            //入队
            clq.add("java");
            clq.add("html");
            clq.add("css");
            //出队
            //clq.poll():出队,并且移除元素
            System.out.println(clq.poll());
            System.out.println(clq.size());
            //clq.peek():出队,但不移除元素
            System.out.println(clq.peek());
            System.out.println(clq.size());
        }
    }
    

    输出

    java
    2
    html
    2
    

    阻塞队列

    import java.util.concurrent.BlockingQueue;
    import java.util.concurrent.LinkedBlockingDeque;
    import java.util.concurrent.TimeUnit;
    
    public class duilieTest02 {
        public static void main(String[] args) throws InterruptedException {
            /*
            查看API得知:BlockingQueue是个接口,只能新建它的子类对象
             */
            BlockingQueue<String> bq=new LinkedBlockingDeque<String>(3);
            bq.offer("java");
            bq.offer("html");
            bq.offer("css");
            System.out.println(bq.poll());
            //3秒内放进去
            bq.offer("oracle",3, TimeUnit.SECONDS);
            System.out.println(bq.poll());
            System.out.println(bq.poll());
            System.out.println(bq.poll());
            System.out.println(bq.poll());
        }
    }
    

    输出

    java
    html
    css
    oracle
    null
    

    线程池的使用

    import java.util.concurrent.LinkedBlockingDeque;
    import java.util.concurrent.ThreadPoolExecutor;
    import java.util.concurrent.TimeUnit;
    
    public class Demo05 {
        public static void main(String[] args) {
            ThreadPoolExecutor pool=new ThreadPoolExecutor(1,2,3, TimeUnit.SECONDS,new LinkedBlockingDeque<>(3));
            System.out.println(Thread.currentThread().getName());
            //利用线程池里的线程开始执行任务
            pool.execute(new ThreadTest());
            pool.execute(new ThreadTest());
            pool.execute(new ThreadTest());
            pool.execute(new ThreadTest());
            pool.execute(new ThreadTest());
    
            //关闭线程池
            pool.shutdown();
        }
        static class ThreadTest implements Runnable{
    
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName());
            }
        }
    }
    

    输出

    main
    pool-1-thread-1
    pool-1-thread-1
    pool-1-thread-1
    pool-1-thread-2
    pool-1-thread-1
    

    线程池的分类

    可缓存

    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    
    public class Demo06 {
        public static void main(String[] args) {
            ExecutorService es= Executors.newCachedThreadPool();
            for (int i = 1; i <=100 ; i++) {
                es.execute(new Runnable() {
                    @Override
                    public void run() {
                        System.out.println(Thread.currentThread().getName());
                    }
                });
            }
        }
    }
    

    输出

    ...
    pool-1-thread-5
    pool-1-thread-14
    pool-1-thread-8
    pool-1-thread-8
    pool-1-thread-5
    pool-1-thread-9
    pool-1-thread-1
    pool-1-thread-2
    pool-1-thread-14
    pool-1-thread-4
    pool-1-thread-4
    pool-1-thread-12
    ...
    一共100条
    

    定长

    核心线程数的数量是固定的。

    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    
    public class Demo07 {
        public static void main(String[] args) {
            ExecutorService es= Executors.newFixedThreadPool(3);
            for (int i = 1; i <=100 ; i++) {
                es.execute(new Runnable() {
                    @Override
                    public void run() {
                        System.out.println(Thread.currentThread().getName());
                    }
                });
            }
        }
    }
    

    输出

    ...
    pool-1-thread-3
    pool-1-thread-1
    pool-1-thread-3
    pool-1-thread-2
    pool-1-thread-3
    pool-1-thread-1
    pool-1-thread-3
    pool-1-thread-2
    pool-1-thread-3
    pool-1-thread-1
    pool-1-thread-3
    pool-1-thread-2
    ...
    一共100条
    

    定时

    public class Demo08 {
        public static void main(String[] args) {
            ScheduledExecutorService ses= Executors.newScheduledThreadPool(3);
            for (int i = 1; i <=100 ; i++) {
                ses.schedule(new Runnable() {
                    @Override
                    public void run() {
                        System.out.println(Thread.currentThread().getName());
                    }
                },3, TimeUnit.SECONDS);
            }
            ses.shutdown();
        }
    }
    

    主程序运行3秒后才开始执行new Runnable()中的语句。

    单例

    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    
    public class Demo09 {
        public static void main(String[] args) {
            ExecutorService es= Executors.newSingleThreadExecutor();
            for (int i = 1; i <=100 ; i++) {
                es.execute(new Runnable() {
                    @Override
                    public void run() {
                        System.out.println(Thread.currentThread().getName());
                    }
                });
            }
        }
    }
    

    输出

    pool-1-thread-1
    pool-1-thread-1
    pool-1-thread-1
    pool-1-thread-1
    pool-1-thread-1
    pool-1-thread-1
    pool-1-thread-1
    pool-1-thread-1
    pool-1-thread-1
    pool-1-thread-1
    pool-1-thread-1
    pool-1-thread-1
    pool-1-thread-1
    pool-1-thread-1
      ...
      100个
    

    线程池处理Callable任务

    public class Demo10  {
        public static void main(String[] args) throws ExecutionException, InterruptedException {
            ExecutorService es=Executors.newFixedThreadPool(3);
            List<Future> list=new ArrayList<Future>();
            for (int i = 1; i <=100 ; i++) {
                Future<String> f = es.submit(new Callable<String>() {
    
                    @Override
                    public String call() throws Exception {
                        Thread.sleep(2000);
                        return Thread.currentThread().getName();
                    }
                });
                list.add(f);
            }
            for (Future future:list) {
                String s = (String)future.get();
                System.out.println(s);
            }
            es.shutdown();
        }
    }
    

    输出

    pool-1-thread-1
    pool-1-thread-2
    pool-1-thread-3
    pool-1-thread-1
    pool-1-thread-2
    pool-1-thread-3
    pool-1-thread-2
    pool-1-thread-1
    pool-1-thread-3
      ...
      输出100个
    
  • 相关阅读:
    python note 30 断点续传
    python note 29 线程创建
    python note 28 socketserver
    python note 27 粘包
    python note 26 socket
    python note 25 约束
    Sed 用法
    python note 24 反射
    python note 23 组合
    python note 22 面向对象成员
  • 原文地址:https://www.cnblogs.com/gaoyao/p/13633824.html
Copyright © 2020-2023  润新知