zoukankan      html  css  js  c++  java
  • java中 ExecutorService,Executor,ThreadPoolExecutor的用法

    package com;
    import java.util.concurrent.BlockingQueue;
    import java.util.concurrent.Executor;
    import java.util.concurrent.ExecutorService;  
    import java.util.concurrent.Executors;  
    import java.util.concurrent.LinkedBlockingQueue;
    import java.util.concurrent.ScheduledExecutorService;
    import java.util.concurrent.ThreadPoolExecutor;
    import java.util.concurrent.TimeUnit;
    
    public class demo3 {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            
             //test1();
             //test2();
             test3();
        }
        
        
        static void test1(){
            //ExecutorService的用法
             ExecutorService fixedThreadPool = Executors.newFixedThreadPool(2);  
              for( int i=0;i<100;i++){
              fixedThreadPool.execute(
                     new Runnable(){
                         
                         public void run() {    
    System.out.println("====threadId="+Thread.currentThread().getId());
                             
                         }
                     }
                      );
              }
            
        
        }
        
        
        static void test2(){
              Executor exec=new ThreadPerTaskExecutor();
              exec.execute(new Runnable(){
    
                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    System.out.println("ccccc"+Thread.currentThread().getId());
                }
                  
                  
              });
              
          }
        
        
        //ThreadPoolExecutor
        static void test3(){
            
            BlockingQueue<Runnable> queue = new LinkedBlockingQueue<Runnable>();
              ThreadPoolExecutor threadPool = new ThreadPoolExecutor(2, 4, 5, TimeUnit.SECONDS,
                        queue, new ThreadPoolExecutor.AbortPolicy());
              
              for(int i=0;i<100;i++){
              threadPool.execute(new MyThread(i+""));
              }
              threadPool.shutdown();
              
        }
        
        
          static class MyThread implements Runnable {
                private String name;
    
                public MyThread(String name) {
                    this.name = name;
                }
    
                @Override
                public void run() {
                    // 做点事情
                    try {
                        Thread.sleep(1000);
                            
                        System.out.println(name+"===当前线程="+Thread.currentThread().getId() + " finished job!") ;
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        
    
    
    }
    
    
    
    //execute的用法
         class ThreadPerTaskExecutor implements Executor {
             public void execute(Runnable r) {
                 new Thread(r).start();
             }
         }
  • 相关阅读:
    springboot的自动配置
    tomcat8.5和redis实现session共享
    GitHub提交时出错,提示需要验证邮箱verify email
    vue3可拖拽容器宽度
    vue解决iOS10-11、vant部分版本ImagePreview点击预览图片无法缩放回去的问题
    使用vue自定义指令限制input输入内容为正整数
    判断当前时间是否超出预约(配送)时间
    vue选择地址字母联动
    手机号码中间四位*号隐藏(别的方法有的机型不适配)
    前端面试题
  • 原文地址:https://www.cnblogs.com/tiancai/p/7146227.html
Copyright © 2011-2022 走看看