//check stop
private boolean isStopped() {
synchronized(_lock){
return _stopFlag;
}
}
}
/*
* Construct an Asynchronous Operate way
*/
class AsynchronousCaller {
private final ThreadPoolExecutor _executor;
/*
* Generate an Thread pool with some thread factory & thread group
*/
public AsynchronousCaller(int poolSize) {
if (poolSize <= 0) {
throw new IllegalArgumentException("illegal pool size: "+poolSize);
}
ThreadGroup threadGroup = new ThreadGroup("Async Caller Group");
ThreadGroupFactory tFactory= new ThreadGroupFactory(threadGroup, "AsyncCaller-");
tFactory.createDaemonThreads(true);
_executor = new ThreadPoolExecutor(poolSize,
poolSize,
Long.MAX_VALUE,
TimeUnit.NANOSECONDS,
new LinkedBlockingQueue(),
tFactory,
new ThreadPoolExecutor.AbortPolicy());
}
public void start() {
_executor.prestartAllCoreThreads();
}
public void stop() {
_executor.shutdown();
}
}
public static void main(String[] args) {
int serverTransNum = 3;
ServerTransport serverTran = new ServerTransport(serverTransNum);
if(!serverTran.isReady()){
try {
serverTran.start();
Thread.sleep(2000);
serverTran.stop();
} catch (Exception e) {
throw new RuntimeException("Unable to start server transport", e);
}
}
}