09-Java5线程并发库的应用

多个任务,线程池中只有3个线程,一个任务做10次

 
使用线程池来管理线程并不是单纯的因为可以限制线程总数这一个功能,如果不用线程池,那么每次要新起一个线程,然后do something,关闭线程.当这个do something只是很简单的hello world,那么每次都要创建线程,hello world,关闭线程,每次在创建线程和关闭线程上有一定的耗时和耗能,性能肯定下降了.
newCachedThreadPool,其实你可以看成newFixedThreadPool(无穷大),虽然无法限制线程总数,但是可以减少不必要的线程创建和销毁上的消耗,如果有批量的hello world过来,当执行第二个任务时第一个任务已经完成,会复用执行第一个任务的线程,如果保持每秒有1000个hello world 请求,则一共会有1000个线程在,但是如果不用线程池,则每秒创建1000个线程,每秒关闭1000个线程
 

使用场景:
1. 耗时较短的任务。
2. 任务处理速度 > 任务提交速度 ,这样才能保证不会不断创建新的进程,避免内存被占满。

取名为cached-threadpool的原因在于线程池中的线程是被线程池缓存了的,也就是说,线程没有任务要执行时,便处于空闲状态,处于空闲状态的线程并不会被立即销毁(会被缓存住),只有当空闲时间超出一段时间(默认为60s)后,线程池才会销毁该线程(相当于清除过时的缓存)。新任务到达后,线程池首先会让被缓存住的线程(空闲状态)去执行任务,如果没有可用线程(无空闲线程),便会创建新的线程。
 
package cn.itcast.demo.thread;

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

public class ThreadPool {
	public static void main(String[] args) {
		ExecutorService threadPool = Executors.newFixedThreadPool(3);	// 固定线程数
		// ExecutorService threadPool = Executors.newSingleThreadExecutor();	// 单个线程服务10个请求
		// ExecutorService threadPool = Executors.newCachedThreadPool();	// 根据线程池中的请求数量指定多少个线程为其服务
		for (int i=1; i<=10; i++) {
			final int task = i;
			threadPool.execute(new Runnable() {
				@Override
				public void run() {
					for (int j=1; j<=10; j++) {
						try {
							Thread.sleep(20);
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
						System.out.println(Thread.currentThread().getName() + " is looping of " + j + ", task: " + task);
					}
				}
			});
		}
		System.out.println("all of 10 tasks has committed!");
		// 结束线程
		threadPool.shutdown();
	}
}

打印结果:

all of 10 tasks has committed!
pool-1-thread-2 is looping of 1, task: 2
pool-1-thread-1 is looping of 1, task: 1
pool-1-thread-3 is looping of 1, task: 3
pool-1-thread-3 is looping of 2, task: 3
pool-1-thread-2 is looping of 2, task: 2
pool-1-thread-1 is looping of 2, task: 1
pool-1-thread-2 is looping of 3, task: 2
pool-1-thread-3 is looping of 3, task: 3
pool-1-thread-1 is looping of 3, task: 1
pool-1-thread-3 is looping of 4, task: 3
pool-1-thread-2 is looping of 4, task: 2
pool-1-thread-1 is looping of 4, task: 1
pool-1-thread-2 is looping of 5, task: 2
pool-1-thread-3 is looping of 5, task: 3
pool-1-thread-1 is looping of 5, task: 1
pool-1-thread-2 is looping of 6, task: 2
pool-1-thread-3 is looping of 6, task: 3
pool-1-thread-1 is looping of 6, task: 1
pool-1-thread-2 is looping of 7, task: 2
pool-1-thread-3 is looping of 7, task: 3
pool-1-thread-1 is looping of 7, task: 1
pool-1-thread-3 is looping of 8, task: 3
pool-1-thread-2 is looping of 8, task: 2
pool-1-thread-1 is looping of 8, task: 1
pool-1-thread-2 is looping of 9, task: 2
pool-1-thread-3 is looping of 9, task: 3
pool-1-thread-1 is looping of 9, task: 1
pool-1-thread-2 is looping of 10, task: 2
pool-1-thread-3 is looping of 10, task: 3
pool-1-thread-1 is looping of 10, task: 1
pool-1-thread-3 is looping of 1, task: 5
pool-1-thread-2 is looping of 1, task: 4
pool-1-thread-1 is looping of 1, task: 6
pool-1-thread-3 is looping of 2, task: 5
pool-1-thread-2 is looping of 2, task: 4
pool-1-thread-1 is looping of 2, task: 6
pool-1-thread-3 is looping of 3, task: 5
pool-1-thread-2 is looping of 3, task: 4
pool-1-thread-1 is looping of 3, task: 6
pool-1-thread-2 is looping of 4, task: 4
pool-1-thread-3 is looping of 4, task: 5
pool-1-thread-1 is looping of 4, task: 6
pool-1-thread-2 is looping of 5, task: 4
pool-1-thread-3 is looping of 5, task: 5
pool-1-thread-1 is looping of 5, task: 6
pool-1-thread-1 is looping of 6, task: 6
pool-1-thread-2 is looping of 6, task: 4
pool-1-thread-3 is looping of 6, task: 5
pool-1-thread-1 is looping of 7, task: 6
pool-1-thread-3 is looping of 7, task: 5
pool-1-thread-2 is looping of 7, task: 4
pool-1-thread-1 is looping of 8, task: 6
pool-1-thread-3 is looping of 8, task: 5
pool-1-thread-2 is looping of 8, task: 4
pool-1-thread-1 is looping of 9, task: 6
pool-1-thread-2 is looping of 9, task: 4
pool-1-thread-3 is looping of 9, task: 5
pool-1-thread-1 is looping of 10, task: 6
pool-1-thread-2 is looping of 10, task: 4
pool-1-thread-3 is looping of 10, task: 5
pool-1-thread-1 is looping of 1, task: 7
pool-1-thread-2 is looping of 1, task: 8
pool-1-thread-3 is looping of 1, task: 9
pool-1-thread-1 is looping of 2, task: 7
pool-1-thread-2 is looping of 2, task: 8
pool-1-thread-3 is looping of 2, task: 9
pool-1-thread-1 is looping of 3, task: 7
pool-1-thread-2 is looping of 3, task: 8
pool-1-thread-3 is looping of 3, task: 9
pool-1-thread-1 is looping of 4, task: 7
pool-1-thread-2 is looping of 4, task: 8
pool-1-thread-3 is looping of 4, task: 9
pool-1-thread-1 is looping of 5, task: 7
pool-1-thread-2 is looping of 5, task: 8
pool-1-thread-3 is looping of 5, task: 9
pool-1-thread-1 is looping of 6, task: 7
pool-1-thread-2 is looping of 6, task: 8
pool-1-thread-3 is looping of 6, task: 9
pool-1-thread-1 is looping of 7, task: 7
pool-1-thread-2 is looping of 7, task: 8
pool-1-thread-3 is looping of 7, task: 9
pool-1-thread-1 is looping of 8, task: 7
pool-1-thread-2 is looping of 8, task: 8
pool-1-thread-3 is looping of 8, task: 9
pool-1-thread-1 is looping of 9, task: 7
pool-1-thread-2 is looping of 9, task: 8
pool-1-thread-3 is looping of 9, task: 9
pool-1-thread-1 is looping of 10, task: 7
pool-1-thread-2 is looping of 10, task: 8
pool-1-thread-3 is looping of 10, task: 9
pool-1-thread-1 is looping of 1, task: 10
pool-1-thread-1 is looping of 2, task: 10
pool-1-thread-1 is looping of 3, task: 10
pool-1-thread-1 is looping of 4, task: 10
pool-1-thread-1 is looping of 5, task: 10
pool-1-thread-1 is looping of 6, task: 10
pool-1-thread-1 is looping of 7, task: 10
pool-1-thread-1 is looping of 8, task: 10
pool-1-thread-1 is looping of 9, task: 10
pool-1-thread-1 is looping of 10, task: 10

每隔多长时间执行一次任务

package cn.itcast.demo.thread;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class ThreadPool {
	public static void main(String[] args) {
		ExecutorService threadPool = Executors.newFixedThreadPool(3);	// 固定线程数
		// ExecutorService threadPool = Executors.newSingleThreadExecutor();	// 单个线程服务10个请求
		// ExecutorService threadPool = Executors.newCachedThreadPool();	// 根据线程池中的请求数量指定多少个线程为其服务
		for (int i=1; i<=10; i++) {
			final int task = i;
			threadPool.execute(new Runnable() {
				@Override
				public void run() {
					for (int j=1; j<=10; j++) {
						try {
							Thread.sleep(20);
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
						System.out.println(Thread.currentThread().getName() + " is looping of " + j + ", task: " + task);
					}
				}
			});
		}
		System.out.println("all of 10 tasks has committed!");
		// 结束线程
		//threadPool.shutdown();
		
		// 定时器:10秒后爆炸
		/*Executors.newScheduledThreadPool(3).schedule(new Runnable() {
			@Override
			public void run() {
				System.out.println("bombing!");
			}
		}, 10, TimeUnit.SECONDS);*/
		
		// 定时器:10秒后爆炸,以后每隔2秒炸一次
		Executors.newScheduledThreadPool(3).scheduleAtFixedRate(new Runnable() {
			@Override
			public void run() {
				System.out.println("------------"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())+"------------");
				System.out.println("bombing!");
			}
		}, 10, 2, TimeUnit.SECONDS);
	}
}

打印结果:

all of 10 tasks has committed!
pool-1-thread-3 is looping of 1, task: 3
pool-1-thread-2 is looping of 1, task: 2
pool-1-thread-1 is looping of 1, task: 1
pool-1-thread-1 is looping of 2, task: 1
pool-1-thread-3 is looping of 2, task: 3
pool-1-thread-2 is looping of 2, task: 2
pool-1-thread-1 is looping of 3, task: 1
pool-1-thread-2 is looping of 3, task: 2
pool-1-thread-3 is looping of 3, task: 3
pool-1-thread-3 is looping of 4, task: 3
pool-1-thread-2 is looping of 4, task: 2
pool-1-thread-1 is looping of 4, task: 1
pool-1-thread-1 is looping of 5, task: 1
pool-1-thread-2 is looping of 5, task: 2
pool-1-thread-3 is looping of 5, task: 3
pool-1-thread-2 is looping of 6, task: 2
pool-1-thread-3 is looping of 6, task: 3
pool-1-thread-1 is looping of 6, task: 1
pool-1-thread-3 is looping of 7, task: 3
pool-1-thread-1 is looping of 7, task: 1
pool-1-thread-2 is looping of 7, task: 2
pool-1-thread-3 is looping of 8, task: 3
pool-1-thread-2 is looping of 8, task: 2
pool-1-thread-1 is looping of 8, task: 1
pool-1-thread-2 is looping of 9, task: 2
pool-1-thread-3 is looping of 9, task: 3
pool-1-thread-1 is looping of 9, task: 1
pool-1-thread-2 is looping of 10, task: 2
pool-1-thread-1 is looping of 10, task: 1
pool-1-thread-3 is looping of 10, task: 3
pool-1-thread-3 is looping of 1, task: 6
pool-1-thread-2 is looping of 1, task: 4
pool-1-thread-1 is looping of 1, task: 5
pool-1-thread-1 is looping of 2, task: 5
pool-1-thread-3 is looping of 2, task: 6
pool-1-thread-2 is looping of 2, task: 4
pool-1-thread-1 is looping of 3, task: 5
pool-1-thread-3 is looping of 3, task: 6
pool-1-thread-2 is looping of 3, task: 4
pool-1-thread-1 is looping of 4, task: 5
pool-1-thread-3 is looping of 4, task: 6
pool-1-thread-2 is looping of 4, task: 4
pool-1-thread-1 is looping of 5, task: 5
pool-1-thread-3 is looping of 5, task: 6
pool-1-thread-2 is looping of 5, task: 4
pool-1-thread-1 is looping of 6, task: 5
pool-1-thread-2 is looping of 6, task: 4
pool-1-thread-3 is looping of 6, task: 6
pool-1-thread-1 is looping of 7, task: 5
pool-1-thread-3 is looping of 7, task: 6
pool-1-thread-2 is looping of 7, task: 4
pool-1-thread-1 is looping of 8, task: 5
pool-1-thread-3 is looping of 8, task: 6
pool-1-thread-2 is looping of 8, task: 4
pool-1-thread-1 is looping of 9, task: 5
pool-1-thread-3 is looping of 9, task: 6
pool-1-thread-2 is looping of 9, task: 4
pool-1-thread-1 is looping of 10, task: 5
pool-1-thread-3 is looping of 10, task: 6
pool-1-thread-2 is looping of 10, task: 4
pool-1-thread-1 is looping of 1, task: 7
pool-1-thread-3 is looping of 1, task: 8
pool-1-thread-2 is looping of 1, task: 9
pool-1-thread-1 is looping of 2, task: 7
pool-1-thread-3 is looping of 2, task: 8
pool-1-thread-2 is looping of 2, task: 9
pool-1-thread-1 is looping of 3, task: 7
pool-1-thread-2 is looping of 3, task: 9
pool-1-thread-3 is looping of 3, task: 8
pool-1-thread-1 is looping of 4, task: 7
pool-1-thread-3 is looping of 4, task: 8
pool-1-thread-2 is looping of 4, task: 9
pool-1-thread-1 is looping of 5, task: 7
pool-1-thread-2 is looping of 5, task: 9
pool-1-thread-3 is looping of 5, task: 8
pool-1-thread-1 is looping of 6, task: 7
pool-1-thread-2 is looping of 6, task: 9
pool-1-thread-3 is looping of 6, task: 8
pool-1-thread-1 is looping of 7, task: 7
pool-1-thread-3 is looping of 7, task: 8
pool-1-thread-2 is looping of 7, task: 9
pool-1-thread-1 is looping of 8, task: 7
pool-1-thread-2 is looping of 8, task: 9
pool-1-thread-3 is looping of 8, task: 8
pool-1-thread-1 is looping of 9, task: 7
pool-1-thread-3 is looping of 9, task: 8
pool-1-thread-2 is looping of 9, task: 9
pool-1-thread-1 is looping of 10, task: 7
pool-1-thread-2 is looping of 10, task: 9
pool-1-thread-3 is looping of 10, task: 8
pool-1-thread-1 is looping of 1, task: 10
pool-1-thread-1 is looping of 2, task: 10
pool-1-thread-1 is looping of 3, task: 10
pool-1-thread-1 is looping of 4, task: 10
pool-1-thread-1 is looping of 5, task: 10
pool-1-thread-1 is looping of 6, task: 10
pool-1-thread-1 is looping of 7, task: 10
pool-1-thread-1 is looping of 8, task: 10
pool-1-thread-1 is looping of 9, task: 10
pool-1-thread-1 is looping of 10, task: 10
------------2017-08-17 16:18:34------------
bombing!
------------2017-08-17 16:18:36------------
bombing!
------------2017-08-17 16:18:38------------
bombing!
------------2017-08-17 16:18:40------------
bombing!
------------2017-08-17 16:18:42------------
bombing!
------------2017-08-17 16:18:44------------
bombing!
------------2017-08-17 16:18:46------------
bombing!
------------2017-08-17 16:18:48------------
bombing!
------------2017-08-17 16:18:50------------
bombing!
------------2017-08-17 16:18:52------------
bombing!
------------2017-08-17 16:18:54------------
bombing!
------------2017-08-17 16:18:56------------
bombing!
------------2017-08-17 16:18:58------------
bombing!
------------2017-08-17 16:19:00------------
bombing!
------------2017-08-17 16:19:02------------
bombing!
------------2017-08-17 16:19:04------------
bombing!

 

posted @ 2017-08-17 16:37  半生戎马,共话桑麻、  阅读(128)  评论(0)    收藏  举报
levels of contents