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;
}
}
}
推荐阅读
-
通俗易懂地给女朋友讲:线程池的内部原理
-
Java中常用的四种线程池
-
.NET线程池最大线程数的限制-记一次IIS并发瓶颈
-
【转】创建线程以及线程池时候要指定与业务相关的名字,以便于追溯问题
-
Android的线程通信:消息机制原理(Message,Handler,MessageQueue,Looper),异步任务AsyncTask,使用JSON
-
Android 入门第十讲02-广播(广播概述,使用方法(系统广播,自定义广播,两个activity之间的交互和传值),EventBus使用方法,数据传递,线程切换,Android的系统广播大全)
-
java线程池:获取运行线程数并控制线程启动速度的方法
-
在spring boot中使用java线程池ExecutorService的讲解
-
java并发编程_线程池的使用方法(详解)
-
基于线程池的工作原理与源码解读