当前位置:首页 > 开发 > 编程语言 > Java > 正文

java deamon 守护进程 实例

发表于: 2012-05-14   作者:blackproof   来源:转载   浏览:
摘要: 守护进程很简单,就是优先级最低的进程 以下是实例   package com.test.deamon; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; /** * DELETE UNEXPECTED THREAD *

守护进程很简单,就是优先级最低的进程

以下是实例

 

package com.test.deamon;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


/**
 * DELETE UNEXPECTED THREAD
 * ADD 2012-05-09
 * @author DIRK.ZHANG
 */
public class Deamon extends Thread{
	
	public final String[] THREADS = {"taskkill /f /im notepad.exe",
										"taskkill /f /im rose.exe",
										"taskkill /f /im chrome.exe"};
	
	public static void main(String[] arg)
	{
		Thread deamon = new Deamon();
		deamon.setDaemon(true);
		deamon.start();
		Thread noDeamon = new MainThread();
		noDeamon.start();
		/*String[] testStr = {"21"};
		System.out.println(testStr.length);*/
	}
	
	@Override
	public void run()
	{
		try {
			while(true)
			{
				for(int i=0;i<THREADS.length;i++)
				{
					Process proc = Runtime.getRuntime().exec(THREADS[i]);
					//Process proc = Runtime.getRuntime().exec("taskkill /f /im notepad.exe");
					BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
					String string_temp;
					while((string_temp = in.readLine()) !=null)
					{
						System.out.println(string_temp);
					}
				}
				Thread.sleep(1000);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
}

   守护进程

   package com.test.deamon;

public class MainThread extends Thread
{
	@Override
	public void run()
	{
		while(true)
		{
			System.out.println("main thread");
			try {
				Thread.sleep(500);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}

  主进程,无实用

 

实例讲述的是用守护进程删除一些进程的故事

java deamon 守护进程 实例

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号