首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

ExecutorService线程池的施用

2012-12-26 
ExecutorService线程池的使用??????? }??????? public Thread newThread(Runnable r) {??????????? Thread

ExecutorService线程池的使用

??????? }

??????? public Thread newThread(Runnable r) {
??????????? Thread t = new Thread(group, r,namePrefix + threadNumber.getAndIncrement(),0);
??????????? if (t.isDaemon())
??????????????? t.setDaemon(false);
??????????? if (t.getPriority() != Thread.NORM_PRIORITY)
??????????????? t.setPriority(Thread.NORM_PRIORITY);
??????????? return t;
??????? }
??? }

也可自己定义ThreadFactory,加入建立池的参数中

?public static ExecutorService newCachedThreadPool(ThreadFactory threadFactory?) {



Executor的execute()方法?
execute() 方法将Runnable实例加入pool中,并进行一些pool size计算和优先级处理
execute() 方法本身在Executor接口中定义,有多个实现类都定义了不同的execute()方法
如ThreadPoolExecutor类(cache,fiexed,single三种池子都是调用它)的execute方法如下:

??? public void execute(Runnable command) {
??????? if (command == null)
??????????? throw new NullPointerException();
??????? if (poolSize >= corePoolSize || !addIfUnderCorePoolSize(command)) {
??????????? if (runState == RUNNING && workQueue.offer(command)) {
??????????????? if (runState != RUNNING || poolSize == 0)
??????????????????? ensureQueuedTaskHandled(command);
??????????? }
??????????? else if (!addIfUnderMaximumPoolSize(command))
??????????????? reject(command); // is shutdown or saturated
??????? }
??? }

?

?

?

?

?

?

?

?

?

?

?

热点排行