java 完成定时执行程序,双线程保活,隐藏黑窗口(dos窗口),结束线程(dos命令结束线程)

1.因为经常使用jboss,然后公司得平台程序加线程会出bug,一个项目大家开发文件集成到一起出bug,没法去找问题。于是决定搞一个针对JBOSS的自动重启,jboss使用bat关闭会留个黑窗口,烦的很。

2.步骤如下:

   使用vbs 启动jboss:

public static void statrRun(){
		Runtime runtime = Runtime.getRuntime();
		try { 
			java.lang.Process process = runtime.exec("cmd /k start runjboss.vbs");
		} catch (IOException e) {
			e.printStackTrace();
		} 
		states="启动JBOSS服务";
		System.out.println("启动JBOSS服务");
	}

 获取(jboss)程序运行的PID 等运行情况

	private static String getJboss() { //String cmd
		 String cmd="tasklist";
        Runtime rt = Runtime.getRuntime();  
        try {
            java.lang.Process proc = rt.exec(cmd); 
            InputStream stderr = proc.getInputStream(); 
            InputStreamReader isr = new InputStreamReader(stderr); 
            BufferedReader br = new BufferedReader(isr); 
            String line = null;
            while ((line = br.readLine()) != null) { 
                if(line.startsWith("java.exe ")||line.startsWith("javax.exe")){
                	 break;
                }
            }
            br.close();
            isr.close();
            stderr.close();
            
            return line;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

得到pid值,并杀死(jboss)程序进程

	public static void stopJboss(){//String pid
		
		String cmd=DoProcess.getJboss();
		String [] c=cmd.split(" ");
		String pid=null;
		for (int i = 0; i < c.length; i++) {
			pid=getPID(c[i]);
			if(pid!=null)break;
		}
		
		Runtime runtime = Runtime.getRuntime();
		try {
			java.lang.Process process = runtime.exec("taskkill /PID " + pid+" /f");
		} catch (IOException e) {
			e.printStackTrace();
		} 
		states="关闭JBOSS进程"+"taskkill /PID " + pid+" /f";
		System.out.println("关闭进程"+"taskkill /PID " + pid+" /f");
	}

就这样愉快的完成了程序的启动死亡。再贴一个比较实用的的线程保命写法(可以自己优化下):

	public long timet=0;
	public long timee=0;
	public String lock=new String();
	
	public RunT runt=new RunT();
	public Rune rune=new Rune();
	
	
	public static void main(String[] args) {
		TRun tr=new TRun();
		tr.start();
		DoProcess.statrRun();
		 
		
		 
	}
	
	public void start(){
		new Thread(runt).start();
		new Thread(rune).start();
	}
	
	
	public class RunT implements Runnable{
		public int k=2;
		
		public void run(){
			while (true) {
				timet=new Date().getTime();
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				String hm=new SimpleDateFormat("HHmm").format(new Date());
				if(hm.endsWith("08:00")){
					DoProcess.stopJboss();
					try {
						Thread.sleep(1000*61);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					DoProcess.statrRun();
				}
				if(hm.endsWith("12:00")){
					DoProcess.stopJboss();
					try {
						Thread.sleep(1000*61);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					DoProcess.statrRun();
				}
				if(hm.endsWith("13:00")){
					DoProcess.stopJboss();
					try {
						Thread.sleep(1000*61);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					DoProcess.statrRun();
				}
				if(hm.endsWith("17:00")){
					DoProcess.stopJboss();
					try {
						Thread.sleep(1000*61);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					DoProcess.statrRun();
				}
				if(hm.endsWith("23:50")){
					DoProcess.stopJboss();
					try {
						Thread.sleep(1000*61);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					DoProcess.statrRun();
				}
				if(hm.endsWith("05:00")){
					DoProcess.stopJboss();
					try {
						Thread.sleep(1000*61);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					DoProcess.statrRun();
				}
				
				if(new Random().nextInt(50)==2)System.out.println(DoProcess.states+",我来了"+new SimpleDateFormat("HH:mm:ss").format(new Date()));
				
				int o=2/k;
				
				dodo();
			}
		}
		
		public void dodo(){
			if(timee!=0){
				long lg=timet-timee;
				int sm=(int)(lg/1000/60);
				if(sm<0)sm=sm*-1;
				if(sm>5){
					rune.k=0;
					try {
						Thread.sleep(1000*60);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					rune=new Rune();
					System.out.println("--------监听活了-------");
					new Thread(rune).start();
				}
			}
		}
	}
	
	public class Rune implements Runnable{
		public int k=2;
		public void run(){
			while (true) {
				timee=new Date().getTime();
				try {
					Thread.sleep(10000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				if(new Random().nextInt(500)==2)System.out.println(DoProcess.states+",监听着:"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
				
				int o=2/k;
				
				dodo();
			}
		}
		public void dodo(){
			if(timet!=0){
				long lg=timet-timee;
				int sm=(int)(lg/1000/60);
				if(sm<0)sm=sm*-1;
				if(sm>5){
					runt.k=0;
					try {
						Thread.sleep(1000*60);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					runt=new RunT();
					System.out.println("》》》》》》》》》》线程活了》》》》》》》》》》》》》");
					new Thread(runt).start();
				}
			}
		}
	}

3.求积分了,代码实例和怎么完成定时自动重启就上链接了。以后有空多上一些这种小实用的代码案例

如果没有链接就是懒得贴了,毕竟上传了还要审核,通过了就懒得贴了

https://download.csdn.net/download/qq_33650242/11072013

 

你可能感兴趣的:(java)