测试
第一步:下面写一个测试入口
package thread;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.ThreadPoolExecutor.DiscardPolicy;
import java.util.concurrent.TimeUnit;
import org.junit.Test;
import threadTest.MyRunable;
/*
* FileName: testThread.java
* Author: cui
* Date: 2018年2月26日 上午11:07:59
*/
/**
* @Author cui.panlong
* @Creation 2018年2月26日 上午11:07:59
* @Description: //模块目的、功能描述
*/
public class testThread {
// 阻塞队列
ArrayBlockingQueue<Runnable> queue = new ArrayBlockingQueue<Runnable>(2);
/**
* 线程池:
* 1:当有线程访问时先检查corePoolSize(线程池的基本大小)是否够用,如果不够用则进入阻塞队列ArrayBlockingQueue
* 2:当阻塞队列满时再查看当前线程池中的线程数是否已经达到了最大值maximumPoolSize(线程池最大大小) 3:如果最大值也已经超过了,那么就通过相应的策略对后续线程的访问做不同的处理
*/
ThreadPoolExecutor threadPool = new ThreadPoolExecutor(1, 3, 0L, TimeUnit.MINUTES, queue, new DiscardPolicy());
@Test
public void testAllByFor() {
for (int i = 1; i < 10; i++) {
threadPool.execute(new MyRunable(String.valueOf(i)));
// 目前阻塞队列里边一共有几个线程被阻塞了
System.out.println(queue.size());
}
}
}
第二步:通过runable接口实现一个执行任务的线程
/*
* FileName: threadTest.java
* Author: cui
* Date: 2018年2月26日 上午10:27:06
*/
package threadTest;
/**
* @Author cui.panlong
* @Creation 2018年2月26日 上午10:27:06
* @Description: //执行任务的线程
*/
public class MyRunable implements Runnable {
String param = "默认";
/**
* @param param
*/
public MyRunable(String param) {
this.param = param;
}
/*
* (non-Javadoc)
* @see java.lang.Runnable#run()
*/
@Override
public void run() {
for (int i = 0; i < 100000000; i++) {
}
System.out.println(this.getClass().getName() + param + "正在运行");
}
}