欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

android上网的线程池研究

程序员文章站 2022-03-22 09:00:38
...
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 ="[email protected]"+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;
}
}

}
相关标签: code