publicvoidexecute(Runnable command) { if (command == null) thrownewNullPointerException(); /* * 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 shouldn't, by returning false. * 1. 如果运行的线程数corePoolSize,尝试开始一个新的线程, * * 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. */ //获取线程池存储的值 intc= ctl.get(); //比较线程池中运行的线程数量与corePoolSize的值 if (workerCountOf(c) < corePoolSize) { //如果少于corePoolSize,新增worker,成功直接返回,如果新增启动成功,直接返回 if (addWorker(command, true)) return; //如果新增失败获取最新的ctl值 c = ctl.get(); } if (isRunning(c) && workQueue.offer(command)) { //如果当前线程状态为运行状态,把当前任务放入队列中
公司准备使用sonar作为代码质量管理,在代码中string不允许直接定义和环境相关的String,只能从配置文件中读取key,需要自己写一个sonar规则。 因为检测的是java文件,直接用java开发个自定义组件。 查看各种规则可以使用的语言:Support of Custom Rules by Language