2.3四種執行緒連線池的配置和使用(和自定義執行緒池)
阿新 • • 發佈:2018-11-14
四種執行緒連線池的配置和使用
最終呼叫類和方法
{引數有 核心執行緒數目,最大執行緒數目,存活時間(當前執行緒執行完這個任務之後,等待下一個任務到來的最長等待時間。如果在這個時間內沒有新的任務來到,那當前執行緒就會退出),時間單位,等待佇列(用於存放待執行的任務)}
public ThreadPoolExecutor(
// 核心執行緒數目
int corePoolSize,
//最大執行緒數目
int maximumPoolSize,
//存活時間(當前執行緒執行完這個任務之後,等待下一個任務到來的最長等待時間。如果在這個時間內沒有新的任務來到,那當前執行緒就會退出)
long keepAliveTime,
//時間單位
TimeUnit unit,
//等待佇列(用於存放待執行的任務)
BlockingQueue<Runnable> workQueue,
//執行緒工廠
ThreadFactory threadFactory,
//處理異常
RejectedExecutionHandler handler){
this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, Executors.defaultThreadFactory(), defaultHandler);}
比較重要的幾個類:
ExecutorService: 真正的執行緒池介面。
ScheduledExecutorService: 能和Timer/TimerTask類似,解決那些需要任務重複執行的問題。
ThreadPoolExecutor: ExecutorService的預設實現。
ScheduledThreadPoolExecutor: 繼承ThreadPoolExecutor的ScheduledExecutorService介面實現,週期性任務排程的類實現
1.newSingleThreadExecutor
/**
* @Auther: cpb
* @Date: 2018/11/12 15:41
* @Description:
*/
public class Solution {
public static void main(String[] args) {
ExecutorService executorService = Executors.newSingleThreadExecutor();
// MyThread myThread = new MyThread();
// A1Thread thread = new A1Thread(myThread);
// for(int i = 0 ; i < 5;i++){
// //執行執行緒
// executorService.submit(thread);
// }
// //結束執行緒
// executorService.shutdown();
MyCallable myCallable = new MyCallable();
for (int i = 0 ; i<5 ;i++){
Future<Integer> future = executorService.submit(myCallable);
try {
System.out.println(future.get());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
executorService.shutdown();
}
static class MyThread implements Runnable{
public void run() {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Hello");
}
}
static class MyCallable implements Callable<Integer> {
private int i;
public Integer call() throws Exception {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
return i++;
}
}
}
2.newFixedThreadPool
import java.util.concurrent.*;
/**
* @Auther: cpb
* @Date: 2018/11/12 15:41
* @Description:
*/
public class Solution {
public static void main(String[] args) {
ExecutorService executorService = Executors.newFixedThreadPool(5);
MyThread myThread = new MyThread();
for(int i = 0 ; i < 10;i++){
Thread thread = new Thread(myThread);
//執行執行緒
executorService.submit(thread);
}
//結束執行緒
executorService.shutdown();
// MyCallable myCallable = new MyCallable();
// for (int i = 0 ; i<10 ;i++){
// Future<Integer> future = executorService.submit(myCallable);
// try {
// System.out.println(future.get());
// } catch (InterruptedException e) {
// e.printStackTrace();
// } catch (ExecutionException e) {
// e.printStackTrace();
// }
// }
// executorService.shutdown();
}
static class MyThread implements Runnable{
public void run() {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName());
System.out.println("Hello");
}
}
static class MyCallable implements Callable<Integer> {
private int i;
public Integer call() throws Exception {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName());
return i++;
}
}
}
3.newCachedThreadPool
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* @Auther: cpb
* @Date: 2018/11/12 15:41
* @Description:
*/
public class Solution {
public static void main(String[] args) {
ExecutorService executorService = Executors.newCachedThreadPool();
MyThread myThread = new MyThread();
for(int i = 0 ; i < 10;i++){
Thread thread = new Thread(myThread);
//執行執行緒
executorService.submit(thread);
}
//結束執行緒
executorService.shutdown();
// MyCallable myCallable = new MyCallable();
// for (int i = 0 ; i<10 ;i++){
// Future<Integer> future = executorService.submit(myCallable);
// try {
// System.out.println(future.get());
// } catch (InterruptedException e) {
// e.printStackTrace();
// } catch (ExecutionException e) {
// e.printStackTrace();
// }
// }
// executorService.shutdown();
}
static class MyThread implements Runnable{
public void run() {
try {
//測試時間60秒 ,有點長啊
Thread.sleep(60000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName());
System.out.println("Hello");
}
}
static class MyCallable implements Callable<Integer> {
private int i;
public Integer call() throws Exception {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName());
return i++;
}
}
}
4.newScheduledThreadPool
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* @Auther: cpb
* @Date: 2018/11/12 15:41
* @Description:
*/
public class Solution {
public static void main(String[] args) {
ExecutorService executorService = Executors.newScheduledThreadPool(5);
MyThread myThread = new MyThread();
for(int i = 0 ; i < 10;i++){
Thread thread = new Thread(myThread);
//執行執行緒
executorService.submit(thread);
}
//結束執行緒
executorService.shutdown();
// MyCallable myCallable = new MyCallable();
// for (int i = 0 ; i<10 ;i++){
// Future<Integer> future = executorService.submit(myCallable);
// try {
// System.out.println(future.get());
// } catch (InterruptedException e) {
// e.printStackTrace();
// } catch (ExecutionException e) {
// e.printStackTrace();
// }
// }
// executorService.shutdown();
}
static class MyThread implements Runnable{
public void run() {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+" :Hello");
}
}
static class MyCallable implements Callable<Integer> {
private int i;
public Integer call() throws Exception {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName());
return i++;
}
}
}
5.自定義執行緒池
執行緒池類
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;
/**
* @Auther: cpb
* @Date: 2018/11/12 17:10
* @Description:
*/
public class MyThreadPool extends ThreadPoolExecutor {
private static final AtomicInteger atomicInteger = new AtomicInteger(0);
public MyThreadPool(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler) {
super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory, handler);
}
public MyThreadPool(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit) {
super(corePoolSize, maximumPoolSize, keepAliveTime, unit,null,null,null);
}
/**
* 任務執行,以原子方式將當前值加 1
*/
public void execute(Runnable task) {
atomicInteger.getAndIncrement();
super.execute(task);
}
}
執行類
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;
/**
* @Auther: cpb
* @Date: 2018/11/12 17:01
* @Description:
*/
public class Solution {
public static void main(String[] args) {
ExecutorService executorService = new MyThreadPool(5,10,60, TimeUnit.SECONDS);
MyThread myThread = new MyThread();
for(int i = 0 ; i < 10;i++){
Thread thread = new Thread(myThread);
//執行執行緒
executorService.submit(thread);
}
//結束執行緒
executorService.shutdown();
// MyCallable myCallable = new MyCallable();
// for (int i = 0 ; i<10 ;i++){
// Future<Integer> future = executorService.submit(myCallable);
// try {
// System.out.println(future.get());
// } catch (InterruptedException e) {
// e.printStackTrace();
// } catch (ExecutionException e) {
// e.printStackTrace();
// }
// }
// executorService.shutdown();
}
static class MyThread implements Runnable{
public void run() {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+" :Hello");
}
}
static class MyCallable implements Callable<Integer> {
private int i;
public Integer call() throws Exception {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName());
return i++;
}
}
}