📄 runprocess.java
字号:
package ffcs.util;
import java.util.LinkedList;
import java.util.List;
/**
* 程序运行类
* @author chenxin
* @version 1.0 $Date 2008-2-14
*/
public class RunProcess {
private List processList = new LinkedList();
private volatile boolean started=false;
/**
*默认构造函数
*/
public RunProcess(){
Runtime.getRuntime().addShutdownHook(new ShutdownHook());
}
/**
* 进程起动方法
* @param args 运行参数
*/
public synchronized void start(String[] args){
if(started){
return;
}
RunProcessInterface process=null;
for(int i=0;i<processList.size();i++){
process = (RunProcessInterface)processList.get(i);
try{
process.callMain(args);
System.out.println("进程["+process.getClass().getName() + "]已起动");
}catch(Exception e){
System.err.println("进程["+process.getClass().getName() + "]起动异常");
e.printStackTrace(System.err);
}
}
started=true;
}
/**
* 添加要运行的进程接口
* @param process 运行的进程接口
*/
public synchronized void addProcess(RunProcessInterface process){
processList.add(process);
}
/**
* 进程停止方法
*/
public synchronized void shutdown(){
RunProcessInterface process=null;
for(int i=0;i<processList.size();i++){
process = (RunProcessInterface)processList.get(i);
process.shutDown();
System.out.println("进程["+process.getClass().getName() + "]已停止");
}
started=false;
}
/**
* 进程停止方法
* @param name 进程名称 对应RunProcessInterface实现类的,类名全称(含包名字)
*/
public synchronized void shutdown(String name){
if(!started){
return;
}
RunProcessInterface process=null;
for(int i=0;i<processList.size();i++){
process = (RunProcessInterface)processList.get(i);
if(process.getClass().getName().equals(name)){
process.shutDown();
System.out.println("进程["+process.getClass().getName() + "]已停止");
}
}
}
/**
* 进程停止的时候会调用该方法
* @author chenxin
* @version 1.0 $Date 2008-2-14
*/
class ShutdownHook extends Thread{
public void run(){
if(started){
shutdown();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -