Threadpool Demo

package com.example.demo.threadPool;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class ThreadPoolDemo {
	
	public static void main(String[] args) throws InterruptedException {
		ThreadPoolExecutor threadPoolExecutor = getThreadPoolExecutor3();
		System.out.println("当前的线程数:"+threadPoolExecutor.getPoolSize());
		for(int i=1;i<16;i++) {
			final int n = i;
			threadPoolExecutor.submit(new Runnable() {
				
				@Override
				public void run() {
					System.out.println("线程"+n+"开始执行");
					try {
						Thread.sleep(3000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					System.out.println("线程"+n+"执行结束");
					
				}
			});
			System.out.println("任务"+i+"提交成功");
		}
		Thread.sleep(500);
		System.out.println("当前的线程数:"+threadPoolExecutor.getPoolSize());
		System.out.println("队列中的线程数:"+threadPoolExecutor.getQueue().size());
		
		Thread.sleep(15000);
		System.out.println("当前的线程数:"+threadPoolExecutor.getPoolSize());
		System.out.println("队列中的线程数:"+threadPoolExecutor.getQueue().size());
		
	}
	
	
	private static ThreadPoolExecutor getThreadPoolExecutor1() {
		ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(5, 10,5, TimeUnit.SECONDS,new LinkedBlockingQueue<Runnable>());
		return threadPoolExecutor;
	}
	
	private static ThreadPoolExecutor getThreadPoolExecutor2() {
		ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(5, 10,5, TimeUnit.SECONDS,new LinkedBlockingQueue<Runnable>(3),new RejectedExecutionHandler() {

			@Override
			public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
				System.err.println("有任务被拒绝了");
			}
			
		});
		return threadPoolExecutor;
	}
	
	private static ThreadPoolExecutor getThreadPoolExecutor3() {
		//Executors.newFixedThreadPool(5);
		//new ThreadPoolExecutor(5, 5,0L, TimeUnit.MILLISECONDS,new LinkedBlockingQueue<Runnable>());
		ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(5, 5,5, TimeUnit.SECONDS,new LinkedBlockingQueue<Runnable>());
		return threadPoolExecutor;
	   
	}
	
	private static ThreadPoolExecutor getThreadPoolExecutor4() {
		//Executors.newCachedThreadPool();
		//new ThreadPoolExecutor(0, Integer.MAX_VALUE,60L, TimeUnit.SECONDS,new SynchronousQueue<Runnable>());
		ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(0, Integer.MAX_VALUE,60, TimeUnit.SECONDS,new SynchronousQueue<Runnable>());
		return threadPoolExecutor;
	   
	}
	
	private static ThreadPoolExecutor getThreadPoolExecutor5() {
		ScheduledThreadPoolExecutor scheduledThreadPoolExecutor = new ScheduledThreadPoolExecutor(5);
		scheduledThreadPoolExecutor.schedule(new Runnable() {
			
			@Override
			public void run() {
				System.out.println("定时任务开始执行");
				
			}
		}, 3, TimeUnit.SECONDS);
		ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(0, Integer.MAX_VALUE,60, TimeUnit.SECONDS,new SynchronousQueue<Runnable>());
		return threadPoolExecutor;
	   
	}
	
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值