码迷,mamicode.com
首页 > 系统相关 > 详细

CachedThreadPool

时间:2018-10-20 14:54:53      阅读:159      评论:0      收藏:0      [点我收藏+]

标签:blog   turn   creating   create   shutdown   lin   ack   1.0   author   

/**
 * <html>
 * <body>
 *  <P> Copyright 1994 JsonInternational</p>
 *  <p> All rights reserved.  - https://github.com/Jasonandy/Java-Core-Advanced </p>
 *  <p> Created by Jason</p>
 *  </body>
 * </html>
 */
package cn.ucaner.core.concurrent;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
* @Package:cn.ucaner.core.concurrent   
* @ClassName:CachedThreadPool   
* @Description:   <p> 
* https://blog.csdn.net/agoodcoolman/article/details/44082181
* https://www.zhihu.com/question/23212914
* 创建一个可缓存线程池,如果线程池长度超过处理需要,可灵活回收空闲线程,若无可回收,则新建线程
* 
* 线程池为无限大,当执行第二个任务时第一个任务已经完成,会复用执行第一个任务的线程,而不用每次新建线程。
*  Star : https://www.cnblogs.com/baizhanshi/p/5469948.html</p>
* @Author: - Jason   
* @CreatTime:2018年5月16日 下午6:04:59   
* @Modify By:   
* @ModifyTime:  2018年5月16日
* @Modify marker:   
* @version    V1.0
 */
public class CachedThreadPool {
    
    public static void main(String[] args) {
        
        ExecutorService exec = Executors.newCachedThreadPool();
        for (int i = 0; i < 5; i++) {
            exec.execute(new LiftOff());
        }
        exec.shutdown();
    }
}
/**
 * <html>
 * <body>
 *  <P> Copyright 1994 JsonInternational</p>
 *  <p> All rights reserved.  - https://github.com/Jasonandy/Java-Core-Advanced </p>
 *  <p> Created by Jason</p>
 *  </body>
 * </html>
 */
package cn.ucaner.core.concurrent;

import java.util.ArrayList;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
* @Package:cn.ucaner.core.concurrent   
* @ClassName:CallableDemo   
* @Description:   <p> CallableDemo </p>
* @Author: -    
* @CreatTime:2018年6月12日 下午3:50:49   
* @Modify By:   
* @ModifyTime:  2018年6月12日
* @Modify marker:   
* @version    V1.0
 */
public class CallableDemo {
    
    public static void main(String[] args) {
        
        ExecutorService exec = Executors.newCachedThreadPool();
        
        ArrayList<Future<String>> results = new ArrayList<>();

        for (int i = 0; i < 10; i++) {
            results.add(exec.submit(new TaskWithResult(i)));
        }

        for (Future<String> fs : results) {
            try {
                System.out.println(fs.get());
            } catch (InterruptedException e) {
                System.out.println(e);
                e.printStackTrace();
            } catch (ExecutionException e) {
                System.out.println(e);
                e.printStackTrace();
            } finally {
                exec.shutdown();
            }
        }
    }
}

class TaskWithResult implements Callable<String> {
    private int id;

    public TaskWithResult(int id) {
        this.id = id;
    }

    @Override
    public String call() throws Exception {
        return "result of TaskWithResult  " + id;
    }
}
//Outputs
//result of TaskWithResult  0
//result of TaskWithResult  1
//result of TaskWithResult  2
//result of TaskWithResult  3
//result of TaskWithResult  4
//result of TaskWithResult  5
//result of TaskWithResult  6
//result of TaskWithResult  7
//result of TaskWithResult  8
//result of TaskWithResult  9
/**
 * <html>
 * <body>
 *  <P> Copyright 1994 JsonInternational</p>
 *  <p> All rights reserved.  - https://github.com/Jasonandy/Java-Core-Advanced </p>
 *  <p> Created by Jason</p>
 *  </body>
 * </html>
 */
package cn.ucaner.core.concurrent;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;

/**
* @Package:cn.ucaner.core.concurrent   
* @ClassName:CaptureUncaughtException   
* @Description:   <p> 捕捉异常</p>
* @Author: - Jason
* @CreatTime:2018年5月16日 下午6:10:37   
* @Modify By:   
* @ModifyTime:  2018年5月16日
* @Modify marker:   
* @version    V1.0
 */
public class CaptureUncaughtException {
    
    public static void main(String[] args) {
        ExecutorService exec = Executors.newCachedThreadPool(
                new HandlerThreadFactory());
        exec.execute(new ExceptionThread());
    }
}

class ExceptionThread implements Runnable {
    public void run() {
        Thread t = Thread.currentThread();
        System.out.println("run() by " + t);
        System.out.println(
                "eh = " + t.getUncaughtExceptionHandler());
        throw new RuntimeException();
    }
}

class MyUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler {
    public void uncaughtException(Thread t, Throwable e) {
        System.out.println("caught " + e + " in " + t);
    }
}

class HandlerThreadFactory implements ThreadFactory {
    public Thread newThread(Runnable r) {
        System.out.println(this + " creating new Thread");
        Thread t = new Thread(r);
        System.out.println("created " + t);
        t.setUncaughtExceptionHandler(
                new MyUncaughtExceptionHandler());
        System.out.println(
                "eh = " + t.getUncaughtExceptionHandler());
        return t;
    }
}



/* Output: (90% match)
com.brianway.learning.java.concurrent.HandlerThreadFactory@266474c2 creating new Thread
created Thread[Thread-0,5,main]
eh = com.brianway.learning.java.concurrent.MyUncaughtExceptionHandler@6f94fa3e
run() by Thread[Thread-0,5,main]
eh = com.brianway.learning.java.concurrent.MyUncaughtExceptionHandler@6f94fa3e
com.brianway.learning.java.concurrent.HandlerThreadFactory@266474c2 creating new Thread
created Thread[Thread-1,5,main]
eh = com.brianway.learning.java.concurrent.MyUncaughtExceptionHandler@3ff961b5
caught java.lang.RuntimeException in Thread[Thread-0,5,main]
*///:~

 

CachedThreadPool

标签:blog   turn   creating   create   shutdown   lin   ack   1.0   author   

原文地址:https://www.cnblogs.com/jasonandy/p/9821509.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!