zoukankan      html  css  js  c++  java
  • 初识多线程

     1 package executors_test;
     2 
     3 import java.util.concurrent.ArrayBlockingQueue;
     4 import java.util.concurrent.ExecutorService;
     5 import java.util.concurrent.Executors;
     6 import java.util.concurrent.RejectedExecutionHandler;
     7 import java.util.concurrent.ScheduledExecutorService;
     8 import java.util.concurrent.ScheduledThreadPoolExecutor;
     9 import java.util.concurrent.ThreadPoolExecutor;
    10 import java.util.concurrent.TimeUnit;
    11 
    12 public class Executors_test {
    13 
    14     public static void main(String[] args) {
    15 
    16         
    17         scheduledThreadPoolExecutorTest();
    18     }
    19 
    20 
    21 
    22     /**
    23      * ScheduledThreadPoolExecutor类可用于定时启动线程,以及周期启动线程
    24      */
    25     public static void scheduledThreadPoolExecutorTest() {
    26         ScheduledThreadPoolExecutor pool = new ScheduledThreadPoolExecutor(2, new MyRejectedExecutionHandler());
    27         pool.scheduleAtFixedRate(new MyThread(5), 10000,1000,TimeUnit.MILLISECONDS);
    28     }
    29 
    30 
    31 
    32     /**
    33      * ThreadPoolExecutor类实现的多线程
    34      */
    35     public static void threadPoolExecutorTest() {
    36         ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(2, 4, 2,TimeUnit.MINUTES, new ArrayBlockingQueue<Runnable>(4),new MyRejectedExecutionHandler());
    37         for (int i = 0; i < 100; i++) {
    38             threadPoolExecutor.execute(new MyThread(i));
    39         }
    40     }
    41 
    42     
    43     
    44     /**
    45      * Executors工厂类实现的多线程,
    46      */
    47     public static void newFixedThreadPoolTest(){
    48         ExecutorService executors = Executors.newFixedThreadPool(2);
    49         for (int i = 0; i < 10; i++) {
    50             executors.execute(new MyThread(i));
    51         }
    52     };
    53     /**
    54      * Executors工厂类实现的多线程,可用于定时启动线程,以及周期启动线程
    55      */
    56     public static void newScheduledThreadPoolTest(){
    57         ScheduledExecutorService executors = Executors.newScheduledThreadPool(2);
    58         executors.scheduleAtFixedRate(new MyThread(5), 10000,1000,TimeUnit.MILLISECONDS);
    59     }
    60     
    61     
    62     static class MyThread implements Runnable {
    63         int id;
    64 
    65         public MyThread(int id) {
    66             this.id = id;
    67         }
    68         @Override
    69         public void run() {
    70             System.out.println(Thread.currentThread().getName()+"........"+id);
    71         }
    72     }
    73     
    74     static class MyRejectedExecutionHandler implements RejectedExecutionHandler{
    75         @Override
    76         public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
    77             System.out.println(r.toString()+"");
    78         }
    79     }
    80 }
    想的都是好
  • 相关阅读:
    小白自动化测试指南
    分布式性能测试框架用例方案设想(二)
    高QPS下的固定QPS模型
    测试自动化最佳实践【译】
    moco框架接口命中率统计实践
    基于docker的分布式性能测试框架功能验证(一)
    编写高质量代码:Web前端开发修炼之道(一)
    JavaScript中点操作符和中括号操作符区别
    Vue脚手架生成及配置
    Npm设置淘宝镜像
  • 原文地址:https://www.cnblogs.com/freezone/p/5068880.html
Copyright © 2011-2022 走看看