ThreadPoolExecutor线程池的使用示例

测试主方法类Run:

线程池中定义3个线程处理100个共享资源(list)。

package t3;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class Run {

	public static void main(String[] args) {

		try {
			ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(3,
					3, 4, TimeUnit.SECONDS,
					new ArrayBlockingQueue<Runnable>(3),
					new ThreadPoolExecutor.DiscardPolicy());

			List<String> list = initList();

			int fromIndex = 0;
			for (int totalSize = list.size(); fromIndex <= totalSize;) {
				int toIndex = fromIndex + 30;
				toIndex = toIndex > totalSize ? totalSize : toIndex;
				List<String> subList = list.subList(fromIndex, toIndex);
				TaskThread task = new TaskThread();
				task.setTaskList(subList);
				threadPoolExecutor.execute(task);
				fromIndex = toIndex;
				Thread.sleep(100);
			}
			
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 添加100个共享资源
	 * @return
	 */
	private static List<String> initList() {
		List<String> list = new ArrayList<String>();
		for (int i = 0; i < 100; i++) {
			list.add("处理资源" + i);
		}
		return list;
	}

}

任务类ThreadPoolTask:

package t3;

import java.util.List;

public class TaskThread implements Runnable {

	private List<String> taskList;
	
	@Override
	public void run(){
		for(String str : getTaskList()){
			System.out.println(Thread.currentThread().getName() + str);
		}
	}
	
	public void setTaskList(List<String> taskList){
		this.taskList = taskList;
	}
	
	public List<String> getTaskList(){
		return taskList;
	}
	
}

运行结果:

pool-1-thread-1处理资源0
pool-1-thread-1处理资源1
pool-1-thread-1处理资源2
pool-1-thread-1处理资源3
pool-1-thread-1处理资源4
pool-1-thread-1处理资源5
pool-1-thread-1处理资源6
pool-1-thread-1处理资源7
pool-1-thread-1处理资源8
pool-1-thread-1处理资源9
pool-1-thread-1处理资源10
pool-1-thread-1处理资源11
pool-1-thread-1处理资源12
pool-1-thread-1处理资源13
pool-1-thread-1处理资源14
pool-1-thread-1处理资源15
pool-1-thread-1处理资源16
pool-1-thread-1处理资源17
pool-1-thread-1处理资源18
pool-1-thread-1处理资源19
pool-1-thread-1处理资源20
pool-1-thread-1处理资源21
pool-1-thread-1处理资源22
pool-1-thread-1处理资源23
pool-1-thread-1处理资源24
pool-1-thread-1处理资源25
pool-1-thread-1处理资源26
pool-1-thread-1处理资源27
pool-1-thread-1处理资源28
pool-1-thread-1处理资源29
pool-1-thread-2处理资源30
pool-1-thread-2处理资源31
pool-1-thread-2处理资源32
pool-1-thread-2处理资源33
pool-1-thread-2处理资源34
pool-1-thread-2处理资源35
pool-1-thread-2处理资源36
pool-1-thread-2处理资源37
pool-1-thread-2处理资源38
pool-1-thread-2处理资源39
pool-1-thread-2处理资源40
pool-1-thread-2处理资源41
pool-1-thread-2处理资源42
pool-1-thread-2处理资源43
pool-1-thread-2处理资源44
pool-1-thread-2处理资源45
pool-1-thread-2处理资源46
pool-1-thread-2处理资源47
pool-1-thread-2处理资源48
pool-1-thread-2处理资源49
pool-1-thread-2处理资源50
pool-1-thread-2处理资源51
pool-1-thread-2处理资源52
pool-1-thread-2处理资源53
pool-1-thread-2处理资源54
pool-1-thread-2处理资源55
pool-1-thread-2处理资源56
pool-1-thread-2处理资源57
pool-1-thread-2处理资源58
pool-1-thread-2处理资源59
pool-1-thread-3处理资源60
pool-1-thread-3处理资源61
pool-1-thread-3处理资源62
pool-1-thread-3处理资源63
pool-1-thread-3处理资源64
pool-1-thread-3处理资源65
pool-1-thread-3处理资源66
pool-1-thread-3处理资源67
pool-1-thread-3处理资源68
pool-1-thread-3处理资源69
pool-1-thread-3处理资源70
pool-1-thread-3处理资源71
pool-1-thread-3处理资源72
pool-1-thread-3处理资源73
pool-1-thread-3处理资源74
pool-1-thread-3处理资源75
pool-1-thread-3处理资源76
pool-1-thread-3处理资源77
pool-1-thread-3处理资源78
pool-1-thread-3处理资源79
pool-1-thread-3处理资源80
pool-1-thread-3处理资源81
pool-1-thread-3处理资源82
pool-1-thread-3处理资源83
pool-1-thread-3处理资源84
pool-1-thread-3处理资源85
pool-1-thread-3处理资源86
pool-1-thread-3处理资源87
pool-1-thread-3处理资源88
pool-1-thread-3处理资源89
pool-1-thread-1处理资源90
pool-1-thread-1处理资源91
pool-1-thread-1处理资源92
pool-1-thread-1处理资源93
pool-1-thread-1处理资源94
pool-1-thread-1处理资源95
pool-1-thread-1处理资源96
pool-1-thread-1处理资源97
pool-1-thread-1处理资源98
pool-1-thread-1处理资源99

可见线程池中3个线程每个线程每次分配30个资源进行处理,互斥处理100个资源。

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值