做互联网的水果妹
当前位置:首页 - 娱乐 >

java多线程学习十六:::自定义创建线程池的好处

2019-10-01来源:大游网
* 线程池的主要作用是为了线程复用,也就是避免了线程的频繁创建,但是
* 最开始的那些线程从何而来的? 那就是ThreadFactory


public interface ThreadFactory {
/**
* Constructs a new {@code Thread}. Implementations may also initialize
* priority, name, daemon status, {@code ThreadGroup}, etc.
*
* @param r a runnable to be executed by new thread instance
* @return constructed thread, or {@code null} if the request to
* create a thread is rejected
*/
Thread newThread(Runnable r);
}

自定义线程池的好处,我们建立了多少个线程池,也可以知道线程的名称,组,以及优先级等信息,甚至可以任性地将所有的线程设置为守护进程..大家看下面的代码

package com.rs.thread;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/**
* 线程池的主要作用是为了线程复用,也就是避免了线程的频繁创建,但是
* 最开始的那些线程从何而来的?那就是ThreadFactory
*/
public class Test03_ThreadFactory extends Thread{
public static void main(String[]args) throws InterruptedException {
Test03_ThreadFactory task = new Test03_ThreadFactory();
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(5, 5, 0, TimeUnit.MICROSECONDS,
new SynchronousQueue(), new ThreadFactory() {
@Override
public Thread newThread(Runnable r) {
Thread thread =new Thread(r);
thread.setDaemon(true);
System.out.println("create>> " + thread);
return thread;
}
});
for (int j =0;j<5;j++){
threadPoolExecutor.execute(task);
}
Thread.sleep(3000);
}
}

虽然jdk帮我们实现稳定的线程池,不过,我们要是想做一些扩展,或者自己定义的一些增强功能,这个时候就要选择到 ThreadPoolExecutor 它提供了

beforeExecute() afterExecute() termminated()来对线程池控制

java多线程学习十六:::自定义创建线程池的好处

看以下简单代码

package com.rs.thread;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/**
* 线程池的主要作用是为了线程复用,也就是避免了线程的频繁创建,但是
* 最开始的那些线程从何而来的?那就是ThreadFactory
*
* beforeExecute() afterExecute() termminated()来对线程池控制
*/
public class Test04_ThreadFactory extends Thread{
public static void main(String[]args) throws InterruptedException {
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(5, 5, 0, TimeUnit.MICROSECONDS,
new SynchronousQueue(), new ThreadFactory() {
@Override
public Thread newThread(Runnable r) {
Thread thread =new Thread(r);
thread.setDaemon(true);
System.out.println("create>> " + thread);
return thread;
}
}){
@Override
protected void beforeExecute(Thread t, Runnable r) {
System.out.println("开始执行>> " + r);
}
@Override
protected void afterExecute(Runnable r, Throwable t) {
System.out.println("执行完成 >> " + r);
}
@Override
protected void terminated() {
System.out.println("线程退出>> " );
}
};
for (int j =0;j<5;j++){
Test03_ThreadFactory task = new Test03_ThreadFactory();
threadPoolExecutor.execute(task);
}
Thread.sleep(3000);
threadPoolExecutor.shutdown();
}
}

合理使用线程池的数量

java多线程学习十六:::自定义创建线程池的好处

转载文章地址:http://www.loleet.com/yule/45555.html
(本文来自做互联网的水果妹整合文章:http://www.loleet.com)未经允许,不得转载!
标签:
Java
网站简介 联系我们 网站申明 网站地图

版权所有:www.loleet.com ?2017 做互联网的水果妹

做互联网的水果妹提供的所有内容均是网络转载或网友提供,本站仅提供内容展示服务,不承认任何法律责任。