• Java 多线程开发之Executors


    java自带进程监控工具

    C:Program FilesJavajdk1.8.0_141injconsole.exe

    例子1 固定数量线程池

    package testExecutors;
    
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    
    public class MyTestExe {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            test1();
        }
        
        private static void test1() {
            // TODO Auto-generated method stub
            ExecutorService pool=Executors.newFixedThreadPool(3);
            Thread t1=new MyThread(1);
            Thread t2=new MyThread(2);
            Thread t3=new MyThread(3);
            Thread t4=new MyThread(4);
            Thread t5=new MyThread(5);
            pool.execute(t1);
            pool.execute(t2);
            pool.execute(t3);
            pool.execute(t4);
            pool.execute(t5);
            pool.shutdown();
        }
        private static void test2() {
            // TODO Auto-generated method stub
            
        }
    }
    class MyThread extends Thread{
        public int no;
        public MyThread(int i) {
            // TODO Auto-generated constructor stub
            this.no=i;
        }
        @Override
        public void run() {
            // TODO Auto-generated method stub
            System.out.println("task"+this.no+" begins...");
            try {
                sleep(8000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            System.out.println("task"+this.no+" ends...");
        }
    }

    说明:任务类MyThread在任务起始打印数据,耗时8秒钟。

    pool只有三个线程可用,在前三个任务执行完毕之后才运行剩下的2个

    运行结果

    task2 begins...
    task1 begins...
    task3 begins...
    task2 ends...
    task1 ends...
    task4 begins...
    task5 begins...
    task3 ends...
    task5 ends...
    task4 ends...
    例子1运行结果

    例子2:单线程线程池

    将test1函数第一行代码改成

     ExecutorService pool = Executors.newSingleThreadExecutor();
    

    运行结果

    一个任务运行结束才开始下一个

    本例子和例子1的相同之处:当加入的线程超过最大尺寸,其他线程需要等待。

    task1 begins...
    task1 ends...
    task2 begins...
    task2 ends...
    task3 begins...
    task3 ends...
    task4 begins...
    task4 ends...
    task5 begins...
    task5 ends...
    例子2运行结果

    例子3:可变线程池

    ExecutorService pool = Executors.newCachedThreadPool();
    

    根据需要创建线程(如果先前创建的线程可用,会复用之)

    60秒没有使用的线程会被终止和移除

    特别适合短寿命的异步任务,

    运行结果:创建了5个线程,几乎同时运行。

    task1 begins...
    task4 begins...
    task3 begins...
    task2 begins...
    task5 begins...
    task4 ends...
    task1 ends...
    task5 ends...
    task3 ends...
    task2 ends...
    View Code

    例子4 延迟执行线程池

    test1中代码改成如下:

    private static void test1() {
            // TODO Auto-generated method stub
            ScheduledExecutorService pool = Executors.newScheduledThreadPool(2);
            Thread t1=new MyThread(1);
            Thread t2=new MyThread(2);
            Thread t3=new MyThread(3);
            Thread t4=new MyThread(4);
            Thread t5=new MyThread(5);
            pool.execute(t1);
            pool.execute(t2);
            pool.execute(t3);
            //pool.execute(t4);
            //pool.execute(t5);
            pool.schedule(t4, 10, TimeUnit.SECONDS);
            pool.schedule(t5,10,TimeUnit.SECONDS);
            pool.shutdown();
        }

    说明:本例子和例子1很相似,线程池中只有2个可用线程。

    任务4和任务5在等待10秒钟之后被使能,

  • 相关阅读:
    ios 动态库加载及某个文件非ARC问题
    block循环引用用__weak声明
    nib加载方式
    UIImage两种加载方式
    xcode头文件编译错误
    ios 工程的相对路径
    ios的window.rootViewController的更换
    Header Search Paths找不到
    mac下svn问题 —— “.a”(静态库)文件无法上传的 简单处理办法
    UIPickerView 和 UIDatePicker常用方法, 程序启动的完整过程
  • 原文地址:https://www.cnblogs.com/legion/p/8820442.html
Copyright © 2020-2023  润新知