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方法如下:
??????? 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
??????? }
??? }
?
?
?
?
?
?
?
?
?
?
?