线程池

固定线程池

public class ThreadPoolDemo1 {
    public static void main(String[] args) {
        ExecutorService service = Executors.newFixedThreadPool(5);
        for(int i=0;i<6;i++){
            service.execute(()->{
                try {
                    TimeUnit.MICROSECONDS.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName());
            });
        }
        System.out.println(service);
        service.shutdown();
        System.out.println(service);
        System.out.println(service.isTerminated());
        System.out.println(service.isShutdown());
        try {
            TimeUnit.SECONDS.sleep(2);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(service);
        System.out.println(service.isTerminated());
        System.out.println(service.isShutdown());

    }
}

future

public class FutureDemo {
    public static void main(String[] args) {
        ExecutorService service= Executors.newFixedThreadPool(5);
       Future<Integer> f= service.submit(()->{
            TimeUnit.MILLISECONDS.sleep(1);
            return 1;
        });
        try {
            System.out.println(f.get());
            System.out.println(f.isDone());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }
}

可缓存线程池

public class CachePoolDemo {
    public static void main(String[] args) throws InterruptedException {
        ExecutorService service= Executors.newCachedThreadPool();
        System.out.println(service);
      for(int i=0;i<3;i++){
          service.submit(()->{
              try {
                  TimeUnit.MILLISECONDS.sleep(500);
              } catch (InterruptedException e) {
                  e.printStackTrace();
              }
              System.out.println(Thread.currentThread().getName());
          });

      }
        System.out.println(service);

      TimeUnit.SECONDS.sleep(60);
        System.out.println(service);

        TimeUnit.SECONDS.sleep(1);
        System.out.println(service);

    }

}

单一线程池

public class SingleThreadPool {
    public static void main(String[] args) {
        ExecutorService service= Executors.newSingleThreadExecutor();
        for(int i=0;i<10;i++){
            final int j=i;
            service.submit(()->{

                System.out.println(""+j+Thread.currentThread().getName());
            });
        }
    }

}

可调度线程池

public class ScheduledPoolDemo {
    public static void main(String[] args) {
       ScheduledExecutorService service= Executors.newScheduledThreadPool(4);
        service.scheduleAtFixedRate(()->{
            try {
                TimeUnit.MILLISECONDS.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName());

        },0,500, TimeUnit.MILLISECONDS);
    }
}

WorkStealingPool

public class Demo11_WorkStealingPool {

   public static void main(String[] args) throws InterruptedException {
      ExecutorService service = Executors.newWorkStealingPool();
      System.out.println(Runtime.getRuntime().availableProcessors());
      
      service.execute(new R(1000));
      service.execute(new R(2000));
      service.execute(new R(2000));
      service.execute(new R(2000));
      service.execute(new R(2000));
      service.execute(new R(2000));
      service.execute(new R(2000));
      service.execute(new R(2000));
      service.execute(new R(2000));
      service.execute(new R(2000));
      service.execute(new R(2000));
      service.execute(new R(2000));
      service.execute(new R(2000));

      TimeUnit.SECONDS.sleep(90);
   }
   
   static class R implements Runnable{

      int time;
      
      public R (int t){
         this.time = t;
      }
      
      @Override
      public void run() {
         try {
            TimeUnit.MILLISECONDS.sleep(time);
         } catch (InterruptedException e) {
            e.printStackTrace();
         }
         System.out.println(time + " " + Thread.currentThread().getName());
      }
      
   }
}

ForkJoinPool

public class Demo12_ForkJoinPool {
   static int[] nums = new int[1000000];
   static final int MAX_NUM = 50000;
   static Random r = new Random();

   static {
      for(int i=0; i<nums.length; i++) {
         nums[i] = r.nextInt(100);
      }
      System.out.println(Arrays.stream(nums).sum());
   }
   
   /*static class AddTask extends RecursiveAction {
      
      int start, end;
      
      AddTask(int s, int e) {
         start = s;
         end = e;
      }

      @Override
      protected void compute() {
         if(end-start <= MAX_NUM) {
            long sum = 0L;
            for(int i=start; i<end; i++){
               sum += nums[i];
            }
            System.out.println(sum);
         } else {
            int middle = start + (end-start)/2;
            AddTask subTask1 = new AddTask(start, middle);
            AddTask subTask2 = new AddTask(middle, end);
            subTask1.fork();
            subTask2.fork();
         }
      }
   }*/

   static class AddTask extends RecursiveTask<Long> {

      int start, end;
      
      AddTask(int s, int e) {
         start = s;
         end = e;
      }

      @Override
      protected Long compute() {
         if(end-start <= MAX_NUM) {
            long sum = 0L;
            for(int i=start; i<end; i++){
               sum += nums[i];
            }
            return sum;
         } 
         int middle = start + (end-start)/2;
         AddTask subTask1 = new AddTask(start, middle);
         AddTask subTask2 = new AddTask(middle, end);
         subTask1.fork();
         subTask2.fork();
         return subTask1.join() + subTask2.join();
      }
   }
   
   public static void main(String[] args) {
      ForkJoinPool forkJoinPool = new ForkJoinPool();
      AddTask task = new AddTask(0, nums.length);
      forkJoinPool.execute(task);
      long result = task.join();
      System.out.println(result);

      try {
         TimeUnit.SECONDS.sleep(10);
      } catch (InterruptedException e) {
         e.printStackTrace();
      }
   }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值