从Java源码看java线程池ITeye - 千亿集团

从Java源码看java线程池ITeye

2019-01-12 00:57:38 | 作者: 怀瑶 | 标签: 一个,简单,搞混 | 浏览: 703

// If pool is stopping, ensure thread is interrupted; // if not, ensure thread is not interrupted. This // requires a recheck in second case to deal with // shutdownNow race while clearing interrupt if ((runStateAtLeast(ctl.get(), STOP) || (Thread.interrupted() runStateAtLeast(ctl.get(), STOP))) !wt.isInterrupted()) wt.interrupt(); try { beforeExecute(wt, task); Throwable thrown = null; try { //简单搞混的点,thread中start()是敞开一个线程,而这儿仅仅只是调用run办法。 task.run(); } catch (RuntimeException x) { thrown = x; throw x; } catch (Error x) { thrown = x; throw x; } catch (Throwable x) { thrown = x; throw new Error(x); } finally { afterExecute(task, thrown); } finally { task = null; w.completedTasks++; w.unlock(); completedAbruptly = false; } finally { processWorkerExit(w, completedAbruptly); //最终或许少一个我们常常看到的进口  public void execute(Runnable command) {         if (command == null)             throw new NullPointerException();         /*          * Proceed in 3 steps:          *          * 1. If fewer than corePoolSize threads are running, try to          * start a new thread with the given command as its first          * task.  The call to addWorker atomically checks runState and          * workerCount, and so prevents false alarms that would add          * threads when it shouldnt, by returning false.          *          * 2. If a task can be successfully queued, then we still need          * to double-check whether we should have added a thread          * (because existing ones died since last checking) or that          * the pool shut down since entry into this method. So we          * recheck state and if necessary roll back the enqueuing if          * stopped, or start a new thread if there are none.          *          * 3. If we cannot queue task, then we try to add a new          * thread.  If it fails, we know we are shut down or saturated          * and so reject the task.          */         int c = ctl.get();         if (workerCountOf(c) corePoolSize) {             if (addWorker(command, true))                 return;             c = ctl.get();         }         if (isRunning(c) workQueue.offer(command)) {             int recheck = ctl.get();             if (! isRunning(recheck) remove(command))                 reject(command);             else if (workerCountOf(recheck) == 0)                 addWorker(null, false);         }         else if (!addWorker(command, false))             reject(command);     }

 

版权声明
本文来源于网络,版权归原作者所有,其内容与观点不代表千亿集团立场。转载文章仅为传播更有价值的信息,如采编人员采编有误或者版权原因,请与我们联系,我们核实后立即修改或删除。

猜您喜欢的文章