import java.io.Serializable;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class Mian1 {
/**
* @param args
*/
private static int produceTaskSleepTime = 2;
private static int consumeTaskSleepTime = 2000;
private static int produceTaskMaxNumber = 30;
public static void main(String[] args) {
// TODO Auto-generated method stub
ThreadPoolExecutor threadPool = new ThreadPoolExecutor(4, 4, 3, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(),new ThreadPoolExecutor.DiscardOldestPolicy());
for(int i=1;i<=produceTaskMaxNumber;i++){
String task ="task@"+i;
System.out.println("put" +task);
threadPool.execute(new ThreadPoolTask(task));
try {
Thread.sleep(produceTaskSleepTime);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println("1111111111");
}
public static class ThreadPoolTask implements Runnable, Serializable {
private static final long serialversionUID = 0;
private Object threadPoolTaskData;
public ThreadPoolTask(Object tasks) {
this.threadPoolTaskData = tasks;
}
@Override
public void run() {
// TODO Auto-generated method stub
System.out.println("start.." + threadPoolTaskData);
try {
Thread.sleep(consumeTaskSleepTime);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
threadPoolTaskData = null;
}
public Object getTask(){
return this.threadPoolTaskData;
}
}
}
android上网的线程池研究
最新推荐文章于 2022-05-30 20:36:52 发布