17 changed files with 1403 additions and 42 deletions
Binary file not shown.
@ -0,0 +1,10 @@ |
|||
package org.nl.acs.device_driver; |
|||
|
|||
public interface HeartbeatableDeviceDriver extends DeviceDriver { |
|||
default void checkHeartbeat() { |
|||
} |
|||
|
|||
default boolean isOnline() { |
|||
return false; |
|||
} |
|||
} |
@ -0,0 +1,92 @@ |
|||
package org.nl.acs.heartbeat; |
|||
|
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.nl.acs.auto.run.AbstractAutoRunnable; |
|||
import org.nl.acs.auto.run.SystemConfig; |
|||
import org.nl.acs.device_driver.HeartbeatableDeviceDriver; |
|||
import org.nl.acs.opc.*; |
|||
import org.springframework.beans.factory.annotation.Autowired; |
|||
import org.springframework.stereotype.Component; |
|||
|
|||
import java.util.*; |
|||
import java.util.concurrent.ExecutorService; |
|||
|
|||
@Slf4j |
|||
@Component |
|||
public class DeviceHeartbeatExecuteAutoRun extends AbstractAutoRunnable { |
|||
|
|||
@Autowired |
|||
DeviceAppService deviceAppService; |
|||
int initial_thread = 2; |
|||
int max_thread = 10; |
|||
int cache_thread = 5; |
|||
int loop_time_millions = 300; |
|||
ExecutorService executorService; |
|||
Map<String, BlockedRunable> runs; |
|||
|
|||
public DeviceHeartbeatExecuteAutoRun() { |
|||
this.executorService = ThreadPoolUtl.newIncreasedBlockedThreadPool(this.initial_thread, this.max_thread, this.cache_thread); |
|||
this.runs = new LinkedHashMap(); |
|||
this.runs = Collections.synchronizedMap(this.runs); |
|||
} |
|||
|
|||
public String getCode() { |
|||
return DeviceHeartbeatExecuteAutoRun.class.getSimpleName(); |
|||
} |
|||
|
|||
public String getName() { |
|||
return "设备在线监听"; |
|||
} |
|||
|
|||
public void autoRun() { |
|||
while(true) { |
|||
ThreadUtl.sleep((long)this.loop_time_millions); |
|||
if (SystemConfig.heartbeat_no_use) { |
|||
return; |
|||
} |
|||
if(!DeviceOpcSynchronizeAutoRun.isRun){ |
|||
return; |
|||
} |
|||
|
|||
List<HeartbeatableDeviceDriver> deviceDrivers = this.deviceAppService.findDeviceDriver(HeartbeatableDeviceDriver.class); |
|||
Iterator var2 = deviceDrivers.iterator(); |
|||
|
|||
while(var2.hasNext()) { |
|||
final HeartbeatableDeviceDriver deviceDriver = (HeartbeatableDeviceDriver)var2.next(); |
|||
if (deviceDriver.getDevice().getIs_config() != null && deviceDriver.getDevice().getIs_config() && !this.runs.keySet().contains(deviceDriver.getDeviceCode())) { |
|||
BlockedRunable runable = new BlockedRunable() { |
|||
public void subRun() { |
|||
deviceDriver.checkHeartbeat(); |
|||
} |
|||
|
|||
public String getCode() { |
|||
return deviceDriver.getDeviceCode(); |
|||
} |
|||
}; |
|||
if (!this.runs.keySet().contains(deviceDriver.getDeviceCode())) { |
|||
this.runs.put(deviceDriver.getDeviceCode(), runable); |
|||
} |
|||
|
|||
runable.setIndex(this.runs); |
|||
this.executorService.submit(runable); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
|
|||
public void after() { |
|||
this.executorService.shutdownNow(); |
|||
this.executorService = ThreadPoolUtl.newIncreasedBlockedThreadPool(this.initial_thread, this.max_thread, this.cache_thread); |
|||
this.runs = new LinkedHashMap(); |
|||
this.runs = Collections.synchronizedMap(this.runs); |
|||
} |
|||
|
|||
public ExecutorService getExecutorService() { |
|||
return this.executorService; |
|||
} |
|||
|
|||
public Map<String, BlockedRunable> getRuns() { |
|||
return this.runs; |
|||
} |
|||
|
|||
} |
@ -0,0 +1,10 @@ |
|||
package org.nl.acs.heartbeat; |
|||
|
|||
public class HeartbeatConfig { |
|||
|
|||
/** |
|||
* 最大心跳时常 |
|||
*/ |
|||
public static Integer max_alive_time_millions = 1000 * 30; |
|||
|
|||
} |
@ -0,0 +1,56 @@ |
|||
package org.nl.acs.heartbeat; |
|||
|
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.nl.acs.auto.run.AbstractAutoRunnable; |
|||
import org.nl.acs.auto.run.AutoRunService; |
|||
import org.nl.acs.heartbeat.service.HeartbeatUnifiedService; |
|||
import org.nl.acs.heartbeat.service_impl.HeartbeatUnifiedServiceimpl; |
|||
import org.nl.acs.opc.ThreadUtl; |
|||
import org.nl.modules.wql.util.SpringContextHolder; |
|||
import org.springframework.beans.factory.annotation.Autowired; |
|||
import org.springframework.stereotype.Component; |
|||
|
|||
import java.util.Date; |
|||
|
|||
@Slf4j |
|||
@Component |
|||
public class HeartbeatOfflineCheckerAutoRun extends AbstractAutoRunnable { |
|||
@Autowired |
|||
AutoRunService autoRunService; |
|||
|
|||
private int recordTimeOut = 10000; |
|||
private Date recordTime; |
|||
int offline_loop_time_millions = 1000; |
|||
boolean heartbeat_no_use =false; |
|||
public HeartbeatOfflineCheckerAutoRun() { |
|||
this.recordTime = new Date((new Date()).getTime() - (long)this.recordTimeOut); |
|||
} |
|||
|
|||
public String getCode() { |
|||
return HeartbeatOfflineCheckerAutoRun.class.getSimpleName(); |
|||
} |
|||
|
|||
public String getName() { |
|||
return "在线监听器"; |
|||
} |
|||
|
|||
public void autoRun() { |
|||
HeartbeatUnifiedService heartbeatUnifiedService = SpringContextHolder.getBean(HeartbeatUnifiedServiceimpl.class); |
|||
|
|||
for(; !heartbeat_no_use; ThreadUtl.sleep((long)offline_loop_time_millions)) { |
|||
try { |
|||
heartbeatUnifiedService.offlineChecker(); |
|||
} catch (Exception var3) { |
|||
Date date = new Date(); |
|||
if (date.getTime() - this.recordTime.getTime() < (long)this.recordTimeOut) { |
|||
log.trace("离线事件异常发生时间因为小于{}毫秒,而被无视", this.recordTime); |
|||
return; |
|||
} |
|||
|
|||
this.recordTime = date; |
|||
log.warn("", var3); |
|||
} |
|||
} |
|||
|
|||
} |
|||
} |
@ -0,0 +1,25 @@ |
|||
package org.nl.acs.opc; |
|||
|
|||
import org.slf4j.Logger; |
|||
import org.slf4j.LoggerFactory; |
|||
|
|||
import java.util.concurrent.RejectedExecutionHandler; |
|||
import java.util.concurrent.ThreadPoolExecutor; |
|||
|
|||
public class BlockedThreadPoolPolicy implements RejectedExecutionHandler { |
|||
private static final Logger log = LoggerFactory.getLogger(BlockedThreadPoolPolicy.class); |
|||
|
|||
public BlockedThreadPoolPolicy() { |
|||
} |
|||
|
|||
public void rejectedExecution(Runnable r, ThreadPoolExecutor e) { |
|||
if (!e.isShutdown()) { |
|||
try { |
|||
e.getQueue().put(r); |
|||
} catch (InterruptedException var4) { |
|||
log.debug("", var4); |
|||
} |
|||
} |
|||
|
|||
} |
|||
} |
@ -0,0 +1,22 @@ |
|||
package org.nl.acs.opc; |
|||
|
|||
import org.slf4j.Logger; |
|||
import org.slf4j.LoggerFactory; |
|||
|
|||
public class IncreasedBlokdedThreadPoolPolicy implements IncreasedRejectedExecutionHandler { |
|||
private static final Logger log = LoggerFactory.getLogger(IncreasedBlokdedThreadPoolPolicy.class); |
|||
|
|||
public IncreasedBlokdedThreadPoolPolicy() { |
|||
} |
|||
|
|||
public void rejectedExecution(Runnable r, IncreasedThreadPoolExecutor e) { |
|||
if (!e.isShutdown()) { |
|||
try { |
|||
e.getQueue().put(r); |
|||
} catch (InterruptedException var4) { |
|||
log.debug("", var4); |
|||
} |
|||
} |
|||
|
|||
} |
|||
} |
@ -0,0 +1,5 @@ |
|||
package org.nl.acs.opc; |
|||
|
|||
public interface IncreasedRejectedExecutionHandler { |
|||
void rejectedExecution(Runnable var1, IncreasedThreadPoolExecutor var2); |
|||
} |
@ -0,0 +1,943 @@ |
|||
package org.nl.acs.opc; |
|||
|
|||
import java.util.*; |
|||
import java.util.concurrent.*; |
|||
import java.util.concurrent.atomic.AtomicInteger; |
|||
import java.util.concurrent.locks.AbstractQueuedSynchronizer; |
|||
import java.util.concurrent.locks.Condition; |
|||
import java.util.concurrent.locks.ReentrantLock; |
|||
|
|||
public class IncreasedThreadPoolExecutor extends AbstractExecutorService { |
|||
private final AtomicInteger ctl; |
|||
private static final int COUNT_BITS = 29; |
|||
private static final int CAPACITY = 536870911; |
|||
private static final int RUNNING = -536870912; |
|||
private static final int SHUTDOWN = 0; |
|||
private static final int STOP = 536870912; |
|||
private static final int TIDYING = 1073741824; |
|||
private static final int TERMINATED = 1610612736; |
|||
private final BlockingQueue<Runnable> workQueue; |
|||
private final ReentrantLock mainLock; |
|||
private final HashSet<Worker> workers; |
|||
private final Condition termination; |
|||
private int largestPoolSize; |
|||
private long completedTaskCount; |
|||
private volatile ThreadFactory threadFactory; |
|||
private volatile IncreasedRejectedExecutionHandler handler; |
|||
private volatile long keepAliveTime; |
|||
private volatile boolean allowCoreThreadTimeOut; |
|||
private volatile int corePoolSize; |
|||
private volatile int maximumPoolSize; |
|||
private volatile int multiple; |
|||
private static final IncreasedRejectedExecutionHandler defaultHandler = new AbortPolicy(); |
|||
private static final RuntimePermission shutdownPerm = new RuntimePermission("modifyThread"); |
|||
private static final boolean ONLY_ONE = true; |
|||
|
|||
private static int runStateOf(int c) { |
|||
return c & -536870912; |
|||
} |
|||
|
|||
private static int workerCountOf(int c) { |
|||
return c & 536870911; |
|||
} |
|||
|
|||
private static int ctlOf(int rs, int wc) { |
|||
return rs | wc; |
|||
} |
|||
|
|||
private static boolean runStateLessThan(int c, int s) { |
|||
return c < s; |
|||
} |
|||
|
|||
private static boolean runStateAtLeast(int c, int s) { |
|||
return c >= s; |
|||
} |
|||
|
|||
private static boolean isRunning(int c) { |
|||
return c < 0; |
|||
} |
|||
|
|||
private boolean compareAndIncrementWorkerCount(int expect) { |
|||
return this.ctl.compareAndSet(expect, expect + 1); |
|||
} |
|||
|
|||
private boolean compareAndDecrementWorkerCount(int expect) { |
|||
return this.ctl.compareAndSet(expect, expect - 1); |
|||
} |
|||
|
|||
private void decrementWorkerCount() { |
|||
while(!this.compareAndDecrementWorkerCount(this.ctl.get())) { |
|||
} |
|||
|
|||
} |
|||
|
|||
private void advanceRunState(int targetState) { |
|||
int c; |
|||
do { |
|||
c = this.ctl.get(); |
|||
} while(!runStateAtLeast(c, targetState) && !this.ctl.compareAndSet(c, ctlOf(targetState, workerCountOf(c)))); |
|||
|
|||
} |
|||
|
|||
final void tryTerminate() { |
|||
while(true) { |
|||
int c = this.ctl.get(); |
|||
if (isRunning(c) || runStateAtLeast(c, 1073741824) || runStateOf(c) == 0 && !this.workQueue.isEmpty()) { |
|||
return; |
|||
} |
|||
|
|||
if (workerCountOf(c) != 0) { |
|||
this.interruptIdleWorkers(true); |
|||
return; |
|||
} |
|||
|
|||
ReentrantLock mainLock = this.mainLock; |
|||
mainLock.lock(); |
|||
|
|||
try { |
|||
if (!this.ctl.compareAndSet(c, ctlOf(1073741824, 0))) { |
|||
continue; |
|||
} |
|||
|
|||
try { |
|||
this.terminated(); |
|||
} finally { |
|||
this.ctl.set(ctlOf(1610612736, 0)); |
|||
this.termination.signalAll(); |
|||
} |
|||
} finally { |
|||
mainLock.unlock(); |
|||
} |
|||
|
|||
return; |
|||
} |
|||
} |
|||
|
|||
private void checkShutdownAccess() { |
|||
SecurityManager security = System.getSecurityManager(); |
|||
if (security != null) { |
|||
security.checkPermission(shutdownPerm); |
|||
ReentrantLock mainLock = this.mainLock; |
|||
mainLock.lock(); |
|||
|
|||
try { |
|||
Iterator var3 = this.workers.iterator(); |
|||
|
|||
while(var3.hasNext()) { |
|||
Worker w = (Worker)var3.next(); |
|||
security.checkAccess(w.thread); |
|||
} |
|||
} finally { |
|||
mainLock.unlock(); |
|||
} |
|||
} |
|||
|
|||
} |
|||
|
|||
private void interruptWorkers() { |
|||
ReentrantLock mainLock = this.mainLock; |
|||
mainLock.lock(); |
|||
|
|||
try { |
|||
Iterator var2 = this.workers.iterator(); |
|||
|
|||
while(var2.hasNext()) { |
|||
Worker w = (Worker)var2.next(); |
|||
w.interruptIfStarted(); |
|||
} |
|||
} finally { |
|||
mainLock.unlock(); |
|||
} |
|||
|
|||
} |
|||
|
|||
private void interruptIdleWorkers(boolean onlyOne) { |
|||
ReentrantLock mainLock = this.mainLock; |
|||
mainLock.lock(); |
|||
|
|||
try { |
|||
Iterator var3 = this.workers.iterator(); |
|||
|
|||
while(var3.hasNext()) { |
|||
Worker w = (Worker)var3.next(); |
|||
Thread t = w.thread; |
|||
if (!t.isInterrupted() && w.tryLock()) { |
|||
try { |
|||
t.interrupt(); |
|||
} catch (SecurityException var15) { |
|||
} finally { |
|||
w.unlock(); |
|||
} |
|||
} |
|||
|
|||
if (onlyOne) { |
|||
break; |
|||
} |
|||
} |
|||
} finally { |
|||
mainLock.unlock(); |
|||
} |
|||
|
|||
} |
|||
|
|||
private void interruptIdleWorkers() { |
|||
this.interruptIdleWorkers(false); |
|||
} |
|||
|
|||
final void reject(Runnable command) { |
|||
this.handler.rejectedExecution(command, this); |
|||
} |
|||
|
|||
void onShutdown() { |
|||
} |
|||
|
|||
final boolean isRunningOrShutdown(boolean shutdownOK) { |
|||
int rs = runStateOf(this.ctl.get()); |
|||
return rs == -536870912 || rs == 0 && shutdownOK; |
|||
} |
|||
|
|||
private List<Runnable> drainQueue() { |
|||
BlockingQueue<Runnable> q = this.workQueue; |
|||
ArrayList<Runnable> taskList = new ArrayList(); |
|||
q.drainTo(taskList); |
|||
if (!q.isEmpty()) { |
|||
Runnable[] var3 = (Runnable[])q.toArray(new Runnable[0]); |
|||
int var4 = var3.length; |
|||
|
|||
for(int var5 = 0; var5 < var4; ++var5) { |
|||
Runnable r = var3[var5]; |
|||
if (q.remove(r)) { |
|||
taskList.add(r); |
|||
} |
|||
} |
|||
} |
|||
|
|||
return taskList; |
|||
} |
|||
|
|||
private boolean addWorker(Runnable firstTask, boolean core) { |
|||
while(true) { |
|||
int c = this.ctl.get(); |
|||
int rs = runStateOf(c); |
|||
if (rs >= 0 && (rs != 0 || firstTask != null || this.workQueue.isEmpty())) { |
|||
return false; |
|||
} |
|||
|
|||
while(true) { |
|||
int wc = workerCountOf(c); |
|||
if (wc >= 536870911 || wc >= (core ? this.corePoolSize : this.maximumPoolSize)) { |
|||
return false; |
|||
} |
|||
|
|||
if (this.compareAndIncrementWorkerCount(c)) { |
|||
boolean workerStarted = false; |
|||
boolean workerAdded = false; |
|||
Worker w = null; |
|||
|
|||
try { |
|||
w = new Worker(firstTask); |
|||
Thread t = w.thread; |
|||
if (t != null) { |
|||
ReentrantLock mainLock = this.mainLock; |
|||
mainLock.lock(); |
|||
|
|||
try { |
|||
int rs1 = runStateOf(this.ctl.get()); |
|||
if (rs1 < 0 || rs1 == 0 && firstTask == null) { |
|||
if (t.isAlive()) { |
|||
throw new IllegalThreadStateException(); |
|||
} |
|||
|
|||
this.workers.add(w); |
|||
int s = this.workers.size(); |
|||
if (s > this.largestPoolSize) { |
|||
this.largestPoolSize = s; |
|||
} |
|||
|
|||
workerAdded = true; |
|||
} |
|||
} finally { |
|||
mainLock.unlock(); |
|||
} |
|||
|
|||
if (workerAdded) { |
|||
t.start(); |
|||
workerStarted = true; |
|||
} |
|||
} |
|||
} finally { |
|||
if (!workerStarted) { |
|||
this.addWorkerFailed(w); |
|||
} |
|||
|
|||
} |
|||
|
|||
return workerStarted; |
|||
} |
|||
|
|||
c = this.ctl.get(); |
|||
if (runStateOf(c) != rs) { |
|||
break; |
|||
} |
|||
} |
|||
} |
|||
} |
|||
|
|||
private void addWorkerFailed(Worker w) { |
|||
ReentrantLock mainLock = this.mainLock; |
|||
mainLock.lock(); |
|||
|
|||
try { |
|||
if (w != null) { |
|||
this.workers.remove(w); |
|||
} |
|||
|
|||
this.decrementWorkerCount(); |
|||
this.tryTerminate(); |
|||
} finally { |
|||
mainLock.unlock(); |
|||
} |
|||
|
|||
} |
|||
|
|||
private void processWorkerExit(Worker w, boolean completedAbruptly) { |
|||
if (completedAbruptly) { |
|||
this.decrementWorkerCount(); |
|||
} |
|||
|
|||
ReentrantLock mainLock = this.mainLock; |
|||
mainLock.lock(); |
|||
|
|||
try { |
|||
this.completedTaskCount += w.completedTasks; |
|||
this.workers.remove(w); |
|||
} finally { |
|||
mainLock.unlock(); |
|||
} |
|||
|
|||
this.tryTerminate(); |
|||
int c = this.ctl.get(); |
|||
if (runStateLessThan(c, 536870912)) { |
|||
if (!completedAbruptly) { |
|||
int min = this.allowCoreThreadTimeOut ? 0 : this.corePoolSize; |
|||
if (min == 0 && !this.workQueue.isEmpty()) { |
|||
min = 1; |
|||
} |
|||
|
|||
if (workerCountOf(c) >= min) { |
|||
return; |
|||
} |
|||
} |
|||
|
|||
this.addWorker((Runnable)null, false); |
|||
} |
|||
|
|||
} |
|||
|
|||
private Runnable getTask() { |
|||
boolean timedOut = false; |
|||
|
|||
while(true) { |
|||
int c = this.ctl.get(); |
|||
int rs = runStateOf(c); |
|||
if (rs >= 0 && (rs >= 536870912 || this.workQueue.isEmpty())) { |
|||
this.decrementWorkerCount(); |
|||
return null; |
|||
} |
|||
|
|||
int wc = workerCountOf(c); |
|||
boolean timed = this.allowCoreThreadTimeOut || wc > this.corePoolSize; |
|||
if (wc <= this.maximumPoolSize && (!timed || !timedOut) || wc <= 1 && !this.workQueue.isEmpty()) { |
|||
try { |
|||
Runnable r = timed ? (Runnable)this.workQueue.poll(this.keepAliveTime, TimeUnit.NANOSECONDS) : (Runnable)this.workQueue.take(); |
|||
if (r != null) { |
|||
return r; |
|||
} |
|||
|
|||
timedOut = true; |
|||
} catch (InterruptedException var7) { |
|||
timedOut = false; |
|||
} |
|||
} else if (this.compareAndDecrementWorkerCount(c)) { |
|||
return null; |
|||
} |
|||
} |
|||
} |
|||
|
|||
final void runWorker(Worker w) { |
|||
Thread wt = Thread.currentThread(); |
|||
Runnable task = w.firstTask; |
|||
w.firstTask = null; |
|||
w.unlock(); |
|||
boolean completedAbruptly = true; |
|||
|
|||
try { |
|||
while(task != null || (task = this.getTask()) != null) { |
|||
w.lock(); |
|||
if ((runStateAtLeast(this.ctl.get(), 536870912) || Thread.interrupted() && runStateAtLeast(this.ctl.get(), 536870912)) && !wt.isInterrupted()) { |
|||
wt.interrupt(); |
|||
} |
|||
|
|||
try { |
|||
this.beforeExecute(wt, task); |
|||
Throwable thrown = null; |
|||
|
|||
try { |
|||
task.run(); |
|||
} catch (RuntimeException var28) { |
|||
thrown = var28; |
|||
throw var28; |
|||
} catch (Error var29) { |
|||
thrown = var29; |
|||
throw var29; |
|||
} catch (Throwable var30) { |
|||
thrown = var30; |
|||
throw new Error(var30); |
|||
} finally { |
|||
this.afterExecute(task, (Throwable)thrown); |
|||
} |
|||
} finally { |
|||
task = null; |
|||
++w.completedTasks; |
|||
w.unlock(); |
|||
} |
|||
} |
|||
|
|||
completedAbruptly = false; |
|||
} finally { |
|||
this.processWorkerExit(w, completedAbruptly); |
|||
} |
|||
|
|||
} |
|||
|
|||
public IncreasedThreadPoolExecutor(int corePoolSize, int maximumPoolSize, int multiple, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue) { |
|||
this(corePoolSize, maximumPoolSize, multiple, keepAliveTime, unit, workQueue, Executors.defaultThreadFactory(), defaultHandler); |
|||
} |
|||
|
|||
public IncreasedThreadPoolExecutor(int corePoolSize, int maximumPoolSize, int multiple, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory) { |
|||
this(corePoolSize, maximumPoolSize, multiple, keepAliveTime, unit, workQueue, threadFactory, defaultHandler); |
|||
} |
|||
|
|||
public IncreasedThreadPoolExecutor(int corePoolSize, int maximumPoolSize, int multiple, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, IncreasedRejectedExecutionHandler handler) { |
|||
this(corePoolSize, maximumPoolSize, multiple, keepAliveTime, unit, workQueue, Executors.defaultThreadFactory(), handler); |
|||
} |
|||
|
|||
public IncreasedThreadPoolExecutor(int corePoolSize, int maximumPoolSize, int multiple, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory, IncreasedRejectedExecutionHandler handler) { |
|||
this.ctl = new AtomicInteger(ctlOf(-536870912, 0)); |
|||
this.mainLock = new ReentrantLock(); |
|||
this.workers = new HashSet(); |
|||
this.termination = this.mainLock.newCondition(); |
|||
if (corePoolSize >= 0 && maximumPoolSize > 0 && maximumPoolSize >= corePoolSize && keepAliveTime >= 0L) { |
|||
if (workQueue != null && threadFactory != null && handler != null) { |
|||
this.corePoolSize = corePoolSize; |
|||
this.maximumPoolSize = maximumPoolSize; |
|||
this.workQueue = workQueue; |
|||
this.keepAliveTime = unit.toNanos(keepAliveTime); |
|||
this.threadFactory = threadFactory; |
|||
this.handler = handler; |
|||
this.multiple = multiple; |
|||
} else { |
|||
throw new NullPointerException(); |
|||
} |
|||
} else { |
|||
throw new IllegalArgumentException(); |
|||
} |
|||
} |
|||
|
|||
public void execute(Runnable command) { |
|||
if (command == null) { |
|||
throw new NullPointerException(); |
|||
} else { |
|||
int c = this.ctl.get(); |
|||
if (workerCountOf(c) < this.corePoolSize) { |
|||
if (this.addWorker(command, true)) { |
|||
return; |
|||
} |
|||
|
|||
c = this.ctl.get(); |
|||
} |
|||
|
|||
if (isRunning(c) && this.workQueue.size() < workerCountOf(c) * this.multiple && this.workQueue.offer(command)) { |
|||
int recheck = this.ctl.get(); |
|||
if (!isRunning(recheck) && this.remove(command)) { |
|||
this.reject(command); |
|||
} else if (workerCountOf(recheck) == 0) { |
|||
this.addWorker((Runnable)null, false); |
|||
} |
|||
} else if (!this.addWorker(command, false)) { |
|||
this.reject(command); |
|||
} |
|||
|
|||
} |
|||
} |
|||
|
|||
public void shutdown() { |
|||
ReentrantLock mainLock = this.mainLock; |
|||
mainLock.lock(); |
|||
|
|||
try { |
|||
this.checkShutdownAccess(); |
|||
this.advanceRunState(0); |
|||
this.interruptIdleWorkers(); |
|||
this.onShutdown(); |
|||
} finally { |
|||
mainLock.unlock(); |
|||
} |
|||
|
|||
this.tryTerminate(); |
|||
} |
|||
|
|||
public List<Runnable> shutdownNow() { |
|||
ReentrantLock mainLock = this.mainLock; |
|||
mainLock.lock(); |
|||
|
|||
List tasks; |
|||
try { |
|||
this.checkShutdownAccess(); |
|||
this.advanceRunState(536870912); |
|||
this.interruptWorkers(); |
|||
tasks = this.drainQueue(); |
|||
} finally { |
|||
mainLock.unlock(); |
|||
} |
|||
|
|||
this.tryTerminate(); |
|||
return tasks; |
|||
} |
|||
|
|||
public boolean isShutdown() { |
|||
return !isRunning(this.ctl.get()); |
|||
} |
|||
|
|||
public boolean isTerminating() { |
|||
int c = this.ctl.get(); |
|||
return !isRunning(c) && runStateLessThan(c, 1610612736); |
|||
} |
|||
|
|||
public boolean isTerminated() { |
|||
return runStateAtLeast(this.ctl.get(), 1610612736); |
|||
} |
|||
|
|||
public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException { |
|||
long nanos = unit.toNanos(timeout); |
|||
ReentrantLock mainLock = this.mainLock; |
|||
mainLock.lock(); |
|||
|
|||
boolean var7; |
|||
try { |
|||
while(!runStateAtLeast(this.ctl.get(), 1610612736)) { |
|||
if (nanos <= 0L) { |
|||
var7 = false; |
|||
return var7; |
|||
} |
|||
|
|||
nanos = this.termination.awaitNanos(nanos); |
|||
} |
|||
|
|||
var7 = true; |
|||
} finally { |
|||
mainLock.unlock(); |
|||
} |
|||
|
|||
return var7; |
|||
} |
|||
|
|||
protected void finalize() { |
|||
this.shutdown(); |
|||
} |
|||
|
|||
public void setThreadFactory(ThreadFactory threadFactory) { |
|||
if (threadFactory == null) { |
|||
throw new NullPointerException(); |
|||
} else { |
|||
this.threadFactory = threadFactory; |
|||
} |
|||
} |
|||
|
|||
public ThreadFactory getThreadFactory() { |
|||
return this.threadFactory; |
|||
} |
|||
|
|||
public void setRejectedExecutionHandler(IncreasedRejectedExecutionHandler handler) { |
|||
if (handler == null) { |
|||
throw new NullPointerException(); |
|||
} else { |
|||
this.handler = handler; |
|||
} |
|||
} |
|||
|
|||
public IncreasedRejectedExecutionHandler getRejectedExecutionHandler() { |
|||
return this.handler; |
|||
} |
|||
|
|||
public void setCorePoolSize(int corePoolSize) { |
|||
if (corePoolSize < 0) { |
|||
throw new IllegalArgumentException(); |
|||
} else { |
|||
int delta = corePoolSize - this.corePoolSize; |
|||
this.corePoolSize = corePoolSize; |
|||
if (workerCountOf(this.ctl.get()) > corePoolSize) { |
|||
this.interruptIdleWorkers(); |
|||
} else if (delta > 0) { |
|||
int k = Math.min(delta, this.workQueue.size()); |
|||
|
|||
while(k-- > 0 && this.addWorker((Runnable)null, true) && !this.workQueue.isEmpty()) { |
|||
} |
|||
} |
|||
|
|||
} |
|||
} |
|||
|
|||
public int getCorePoolSize() { |
|||
return this.corePoolSize; |
|||
} |
|||
|
|||
public boolean prestartCoreThread() { |
|||
return workerCountOf(this.ctl.get()) < this.corePoolSize && this.addWorker((Runnable)null, true); |
|||
} |
|||
|
|||
void ensurePrestart() { |
|||
int wc = workerCountOf(this.ctl.get()); |
|||
if (wc < this.corePoolSize) { |
|||
this.addWorker((Runnable)null, true); |
|||
} else if (wc == 0) { |
|||
this.addWorker((Runnable)null, false); |
|||
} |
|||
|
|||
} |
|||
|
|||
public int prestartAllCoreThreads() { |
|||
int n; |
|||
for(n = 0; this.addWorker((Runnable)null, true); ++n) { |
|||
} |
|||
|
|||
return n; |
|||
} |
|||
|
|||
public boolean allowsCoreThreadTimeOut() { |
|||
return this.allowCoreThreadTimeOut; |
|||
} |
|||
|
|||
public void allowCoreThreadTimeOut(boolean value) { |
|||
if (value && this.keepAliveTime <= 0L) { |
|||
throw new IllegalArgumentException("Core threads must have nonzero keep alive times"); |
|||
} else { |
|||
if (value != this.allowCoreThreadTimeOut) { |
|||
this.allowCoreThreadTimeOut = value; |
|||
if (value) { |
|||
this.interruptIdleWorkers(); |
|||
} |
|||
} |
|||
|
|||
} |
|||
} |
|||
|
|||
public void setMaximumPoolSize(int maximumPoolSize) { |
|||
if (maximumPoolSize > 0 && maximumPoolSize >= this.corePoolSize) { |
|||
this.maximumPoolSize = maximumPoolSize; |
|||
if (workerCountOf(this.ctl.get()) > maximumPoolSize) { |
|||
this.interruptIdleWorkers(); |
|||
} |
|||
|
|||
} else { |
|||
throw new IllegalArgumentException(); |
|||
} |
|||
} |
|||
|
|||
public int getMaximumPoolSize() { |
|||
return this.maximumPoolSize; |
|||
} |
|||
|
|||
public void setKeepAliveTime(long time, TimeUnit unit) { |
|||
if (time < 0L) { |
|||
throw new IllegalArgumentException(); |
|||
} else if (time == 0L && this.allowsCoreThreadTimeOut()) { |
|||
throw new IllegalArgumentException("Core threads must have nonzero keep alive times"); |
|||
} else { |
|||
long keepAliveTime = unit.toNanos(time); |
|||
long delta = keepAliveTime - this.keepAliveTime; |
|||
this.keepAliveTime = keepAliveTime; |
|||
if (delta < 0L) { |
|||
this.interruptIdleWorkers(); |
|||
} |
|||
|
|||
} |
|||
} |
|||
|
|||
public long getKeepAliveTime(TimeUnit unit) { |
|||
return unit.convert(this.keepAliveTime, TimeUnit.NANOSECONDS); |
|||
} |
|||
|
|||
public BlockingQueue<Runnable> getQueue() { |
|||
return this.workQueue; |
|||
} |
|||
|
|||
public boolean remove(Runnable task) { |
|||
boolean removed = this.workQueue.remove(task); |
|||
this.tryTerminate(); |
|||
return removed; |
|||
} |
|||
|
|||
public void purge() { |
|||
BlockingQueue<Runnable> q = this.workQueue; |
|||
|
|||
try { |
|||
Iterator<Runnable> it = q.iterator(); |
|||
|
|||
while(it.hasNext()) { |
|||
Runnable r = (Runnable)it.next(); |
|||
if (r instanceof Future && ((Future)r).isCancelled()) { |
|||
it.remove(); |
|||
} |
|||
} |
|||
} catch (ConcurrentModificationException var7) { |
|||
Object[] var3 = q.toArray(); |
|||
int var4 = var3.length; |
|||
|
|||
for(int var5 = 0; var5 < var4; ++var5) { |
|||
Object r = var3[var5]; |
|||
if (r instanceof Future && ((Future)r).isCancelled()) { |
|||
q.remove(r); |
|||
} |
|||
} |
|||
} |
|||
|
|||
this.tryTerminate(); |
|||
} |
|||
|
|||
public int getPoolSize() { |
|||
ReentrantLock mainLock = this.mainLock; |
|||
mainLock.lock(); |
|||
|
|||
int var2; |
|||
try { |
|||
var2 = runStateAtLeast(this.ctl.get(), 1073741824) ? 0 : this.workers.size(); |
|||
} finally { |
|||
mainLock.unlock(); |
|||
} |
|||
|
|||
return var2; |
|||
} |
|||
|
|||
public int getActiveCount() { |
|||
ReentrantLock mainLock = this.mainLock; |
|||
mainLock.lock(); |
|||
|
|||
try { |
|||
int n = 0; |
|||
Iterator var3 = this.workers.iterator(); |
|||
|
|||
while(var3.hasNext()) { |
|||
Worker w = (Worker)var3.next(); |
|||
if (w.isLocked()) { |
|||
++n; |
|||
} |
|||
} |
|||
|
|||
int var8 = n; |
|||
return var8; |
|||
} finally { |
|||
mainLock.unlock(); |
|||
} |
|||
} |
|||
|
|||
public int getLargestPoolSize() { |
|||
ReentrantLock mainLock = this.mainLock; |
|||
mainLock.lock(); |
|||
|
|||
int var2; |
|||
try { |
|||
var2 = this.largestPoolSize; |
|||
} finally { |
|||
mainLock.unlock(); |
|||
} |
|||
|
|||
return var2; |
|||
} |
|||
|
|||
public long getTaskCount() { |
|||
ReentrantLock mainLock = this.mainLock; |
|||
mainLock.lock(); |
|||
|
|||
try { |
|||
long n = this.completedTaskCount; |
|||
Iterator var4 = this.workers.iterator(); |
|||
|
|||
while(var4.hasNext()) { |
|||
Worker w = (Worker)var4.next(); |
|||
n += w.completedTasks; |
|||
if (w.isLocked()) { |
|||
++n; |
|||
} |
|||
} |
|||
|
|||
long var9 = n + (long)this.workQueue.size(); |
|||
return var9; |
|||
} finally { |
|||
mainLock.unlock(); |
|||
} |
|||
} |
|||
|
|||
public long getCompletedTaskCount() { |
|||
ReentrantLock mainLock = this.mainLock; |
|||
mainLock.lock(); |
|||
|
|||
try { |
|||
long n = this.completedTaskCount; |
|||
|
|||
Worker w; |
|||
for(Iterator var4 = this.workers.iterator(); var4.hasNext(); n += w.completedTasks) { |
|||
w = (Worker)var4.next(); |
|||
} |
|||
|
|||
long var9 = n; |
|||
return var9; |
|||
} finally { |
|||
mainLock.unlock(); |
|||
} |
|||
} |
|||
|
|||
public String toString() { |
|||
ReentrantLock mainLock = this.mainLock; |
|||
mainLock.lock(); |
|||
|
|||
long ncompleted; |
|||
int nworkers; |
|||
int nactive; |
|||
try { |
|||
ncompleted = this.completedTaskCount; |
|||
nactive = 0; |
|||
nworkers = this.workers.size(); |
|||
Iterator var6 = this.workers.iterator(); |
|||
|
|||
while(var6.hasNext()) { |
|||
Worker w = (Worker)var6.next(); |
|||
ncompleted += w.completedTasks; |
|||
if (w.isLocked()) { |
|||
++nactive; |
|||
} |
|||
} |
|||
} finally { |
|||
mainLock.unlock(); |
|||
} |
|||
|
|||
int c = this.ctl.get(); |
|||
String rs = runStateLessThan(c, 0) ? "Running" : (runStateAtLeast(c, 1610612736) ? "Terminated" : "Shutting down"); |
|||
return super.toString() + "[" + rs + ", pool size = " + nworkers + ", active threads = " + nactive + ", queued tasks = " + this.workQueue.size() + ", completed tasks = " + ncompleted + "]"; |
|||
} |
|||
|
|||
protected void beforeExecute(Thread t, Runnable r) { |
|||
} |
|||
|
|||
protected void afterExecute(Runnable r, Throwable t) { |
|||
} |
|||
|
|||
protected void terminated() { |
|||
} |
|||
|
|||
public static class DiscardOldestPolicy implements IncreasedRejectedExecutionHandler { |
|||
public DiscardOldestPolicy() { |
|||
} |
|||
|
|||
public void rejectedExecution(Runnable r, IncreasedThreadPoolExecutor e) { |
|||
if (!e.isShutdown()) { |
|||
e.getQueue().poll(); |
|||
e.execute(r); |
|||
} |
|||
|
|||
} |
|||
} |
|||
|
|||
public static class DiscardPolicy implements IncreasedRejectedExecutionHandler { |
|||
public DiscardPolicy() { |
|||
} |
|||
|
|||
public void rejectedExecution(Runnable r, IncreasedThreadPoolExecutor e) { |
|||
} |
|||
} |
|||
|
|||
public static class AbortPolicy implements IncreasedRejectedExecutionHandler { |
|||
public AbortPolicy() { |
|||
} |
|||
|
|||
public void rejectedExecution(Runnable r, IncreasedThreadPoolExecutor e) { |
|||
throw new RejectedExecutionException("Task " + r.toString() + " rejected from " + e.toString()); |
|||
} |
|||
} |
|||
|
|||
public static class CallerRunsPolicy implements IncreasedRejectedExecutionHandler { |
|||
public CallerRunsPolicy() { |
|||
} |
|||
|
|||
public void rejectedExecution(Runnable r, IncreasedThreadPoolExecutor e) { |
|||
if (!e.isShutdown()) { |
|||
r.run(); |
|||
} |
|||
|
|||
} |
|||
} |
|||
|
|||
private final class Worker extends AbstractQueuedSynchronizer implements Runnable { |
|||
private static final long serialVersionUID = 6138294804551838833L; |
|||
final Thread thread; |
|||
Runnable firstTask; |
|||
volatile long completedTasks; |
|||
|
|||
Worker(Runnable firstTask) { |
|||
this.setState(-1); |
|||
this.firstTask = firstTask; |
|||
this.thread = IncreasedThreadPoolExecutor.this.getThreadFactory().newThread(this); |
|||
} |
|||
|
|||
public void run() { |
|||
IncreasedThreadPoolExecutor.this.runWorker(this); |
|||
} |
|||
|
|||
protected boolean isHeldExclusively() { |
|||
return this.getState() != 0; |
|||
} |
|||
|
|||
protected boolean tryAcquire(int unused) { |
|||
if (this.compareAndSetState(0, 1)) { |
|||
this.setExclusiveOwnerThread(Thread.currentThread()); |
|||
return true; |
|||
} else { |
|||
return false; |
|||
} |
|||
} |
|||
|
|||
protected boolean tryRelease(int unused) { |
|||
this.setExclusiveOwnerThread((Thread)null); |
|||
this.setState(0); |
|||
return true; |
|||
} |
|||
|
|||
public void lock() { |
|||
this.acquire(1); |
|||
} |
|||
|
|||
public boolean tryLock() { |
|||
return this.tryAcquire(1); |
|||
} |
|||
|
|||
public void unlock() { |
|||
this.release(1); |
|||
} |
|||
|
|||
public boolean isLocked() { |
|||
return this.isHeldExclusively(); |
|||
} |
|||
|
|||
void interruptIfStarted() { |
|||
Thread t; |
|||
if (this.getState() >= 0 && (t = this.thread) != null && !t.isInterrupted()) { |
|||
try { |
|||
t.interrupt(); |
|||
} catch (SecurityException var3) { |
|||
} |
|||
} |
|||
|
|||
} |
|||
} |
|||
} |
|||
|
@ -0,0 +1,71 @@ |
|||
package org.nl.acs.opc; |
|||
|
|||
import java.util.concurrent.*; |
|||
|
|||
public class ThreadPoolUtl { |
|||
private static ExecutorService eventExecutorService = null; |
|||
private static ExecutorService cycleExecutorService = null; |
|||
|
|||
private ThreadPoolUtl() { |
|||
} |
|||
|
|||
public static ExecutorService getEventSingleInstanse() { |
|||
if (eventExecutorService == null) { |
|||
Class var0 = ThreadPoolUtl.class; |
|||
synchronized(ThreadPoolUtl.class) { |
|||
if (eventExecutorService == null) { |
|||
eventExecutorService = newIncreasedBlockedThreadPool(50, 200, 5); |
|||
} |
|||
} |
|||
} |
|||
|
|||
return eventExecutorService; |
|||
} |
|||
|
|||
public static ExecutorService getCycleSingleInstanse() { |
|||
if (cycleExecutorService == null) { |
|||
Class var0 = ThreadPoolUtl.class; |
|||
synchronized(ThreadPoolUtl.class) { |
|||
if (cycleExecutorService == null) { |
|||
cycleExecutorService = newIncreasedBlockedThreadPool(5, 200, 5); |
|||
} |
|||
} |
|||
} |
|||
|
|||
return cycleExecutorService; |
|||
} |
|||
|
|||
public static ExecutorService newIncreasedThreadPool() { |
|||
return newIncreasedThreadPool(50, 200, 5); |
|||
} |
|||
|
|||
public static ExecutorService newIncreasedBlockedThreadPool() { |
|||
return newIncreasedBlockedThreadPool(50, 200, 5); |
|||
} |
|||
|
|||
public static ExecutorService newIncreasedThreadPool(int corePoolSize, int maximumPoolSize, int multiple) { |
|||
return new IncreasedThreadPoolExecutor(corePoolSize, maximumPoolSize, multiple, 1L, TimeUnit.HOURS, new LinkedBlockingDeque(), new IncreasedBlokdedThreadPoolPolicy()); |
|||
} |
|||
|
|||
public static ExecutorService newIncreasedBlockedThreadPool(int corePoolSize, int maximumPoolSize, int multiple) { |
|||
int queueLength = maximumPoolSize * multiple; |
|||
return new IncreasedThreadPoolExecutor(corePoolSize, maximumPoolSize, multiple, 1L, TimeUnit.HOURS, new ArrayBlockingQueue(queueLength), new IncreasedBlokdedThreadPoolPolicy()); |
|||
} |
|||
|
|||
public static ExecutorService newBlockedThreadPool() { |
|||
return newBlockedThreadPool(50, 300, 200); |
|||
} |
|||
|
|||
public static ExecutorService newBlockedThreadPool(int coreNum, int maxNum, int cacheNum) { |
|||
return new ThreadPoolExecutor(coreNum, maxNum, 1L, TimeUnit.HOURS, new ArrayBlockingQueue(cacheNum), new BlockedThreadPoolPolicy()); |
|||
} |
|||
|
|||
public static ExecutorService newCachedThreadPool() { |
|||
return Executors.newCachedThreadPool(); |
|||
} |
|||
|
|||
public static ExecutorService newFixedThreadPool(int maxNum) { |
|||
return Executors.newFixedThreadPool(maxNum); |
|||
} |
|||
|
|||
} |
Loading…
Reference in new issue