Executors创建线程池的四种方法

这个类里面对于四种创建连接池的方法都做了详细的描述,可以先看看!
package thread;

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

/**
 * 测试Executors创建的线程池
 * @author likexin
 *
 */
public class TestExecutors {
	
	public static void main(String[] args) {
		
		/*1.Executors.newCachedThreadPool();
		      根据需要创建新线程的线程池,但是将会重用之前构造的可以使用的线程。 这些池通常会提高性能执行许多短暂异步任务的程序。
		      调用execute()将重用以前构造的线程(前提是以前的线程可用)。 
		      如果没有现有的线程可用,一个新的线程将被创建并添加到池中。 线程未被未使用60秒将被终止并且从缓存中删除。
		 */
		ExecutorService exec1 = Executors.newCachedThreadPool();
		ThreadFactory threadFactory1 = new ThreadFactory() {
			
			public Thread newThread(Runnable r) {
				// 你要执行的操作
				return null;
			}
		};
		ExecutorService exec2 = Executors.newCachedThreadPool(threadFactory1);
		
		/*2.Executors.newFixedThreadPool(1)
		 * 创建重用固定数量线程的线程池,并且关闭共享无界队列。 
		 * 在任何时候,最多nThreads线程将是活动处理任务。
         * 如果在所有线程处于活动状态时提交其他任务,他们将在队列中等待,直到线程可用。
         * 如果任何线程由于故障在执行期间终止,如果需要执行后续任务,一个新的将顶替它的位置。
         * 池中的线程将存在,直到明确关掉它。
		 */
		ExecutorService exec3 = Executors.newFixedThreadPool(5);
		ThreadFactory threadFactory2 = new ThreadFactory() {
			
			public Thread newThread(Runnable r) {
				// 你要执行的操作
				return null;
			}
		};
		ExecutorService exec4 = Executors.newFixedThreadPool(1, threadFactory2);
		
		/*3.Executors.newSingleThreadExecutor()
		 * 创建使用单个工作线程操作的Executor,并且关闭无界队列。 
		 * (但请注意,如果这个单线程由于在执行之前的故障而终止关闭,一个新的将取代它的位置,用来执行后续任务。)
		 */
		ExecutorService exec5 = Executors.newSingleThreadExecutor();
		ThreadFactory threadFactory3 = new ThreadFactory() {
			
			public Thread newThread(Runnable r) {
				// 你要执行的操作
				return null;
			}
		};
		ExecutorService exec6 = Executors.newSingleThreadExecutor(threadFactory3);
		
		/*4.Executors.newScheduledThreadPool(5)
		 * 创建一个线程池,它可以安排在给定延迟后运行命令或者定期地执行
		 */
		ExecutorService exec7 = Executors.newScheduledThreadPool(5);
		ThreadFactory threadFactory4 = new ThreadFactory() {
			
			public Thread newThread(Runnable r) {
				// 你要执行的操作
				return null;
			}
		};
		ExecutorService exec8 = Executors.newScheduledThreadPool(5, threadFactory4);
	}

}

下面这个类是根据上个类写的测试的代码,可以运行看看有个直观的感受,具体在写代码 的时候要用哪种要根据具体的业务逻辑:

 

package thread;

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

public class TestSemaphore {

	public static void main(String[] args) {
//		ExecutorService exec = Executors.newCachedThreadPool();
//		ExecutorService exec3 = Executors.newFixedThreadPool(5);
//		ExecutorService exec5 = Executors.newSingleThreadExecutor();
		ExecutorService exec7 = Executors.newScheduledThreadPool(5);
		final Semaphore semp = new Semaphore(5);
		for(int i = 0;i<30;i++){
			Runnable run = new Runnable() {
				
				public void run() {
					try {
						System.out.println(Thread.currentThread());
					} catch (Exception e) {
						
					}
					
				}
			};
			exec7.execute(run);
		}
		exec7.shutdown();
	}
	
}

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值