在Java中查找进程数量

huangapple go评论93阅读模式
英文:

Find Process Count in Java

问题

我可以使用以下命令启动Process,在启动多个进程后,我想控制在某一点上要保留多少个进程。

例如:

  1. 在0到50的范围内,在for循环中启动一个Process
  2. 当总活动进程为5时,暂停for循环。
  3. 一旦从5下降到4或3,恢复for循环。

我尝试了下面的代码,但是似乎漏掉了一些内容。

public class OpenTerminal {

    public static void main(String[] args) throws Exception {

        int counter = 0;

        for (int i = 0; i < 50; i++) {
            while (counter < 5) {
                if (runTheProc().isAlive()) {
                    counter = counter + 1;
                } else if (!runTheProc().isAlive()) {
                    counter = counter - 1;
                }
            }

        }

    }

    private static Process runTheProc() throws Exception {
        return Runtime.getRuntime().exec("cmd /c start cmd.exe /c \"dir && ping localhost\"");
    }

}

此外,如何找出有多少个活动进程?以便我可以同时控制活动进程数量。

英文:

I am able to launch Process with the help of below command and after launching multiple processes I want to control how many processes I want to keep at some point.

For example:

  1. Initiate a Process inside a for loop of range 0 to 50
  2. Pause the for loop once total active processes are 5
  3. Resume for loop once it drop from 5 to 4 or 3 ...

I tried below code, but I am missing something.

public class OpenTerminal {

	public static void main(String[] args) throws Exception {

		int counter = 0;

		for (int i = 0; i &lt; 50; i++) {
			while (counter &lt; 5) {
				if (runTheProc().isAlive()) {
					counter = counter + 1;
				}else if(!runTheProc().isAlive()) {
					counter = counter-1;
				}
			}

		}

	}

	private static Process runTheProc() throws Exception {
		return Runtime.getRuntime().exec(&quot;cmd /c start cmd.exe /c \&quot;dir &amp;&amp; ping localhost\&quot;&quot;);
	}
	
}

Also, how to find out how many process are active? So that I can control active processes at a time.

答案1

得分: 1

你可以使用具有固定大小的线程池。
例如:

public static void main(String[] args) throws Exception {
    ExecutorService threadPool = Executors.newFixedThreadPool(5);

    for (int i = 0; i < 50; i++) {
        threadPool.submit(runTheProc);
    }
}

private static final Runnable runTheProc = () -> {
    Process process;
    try {
        process = Runtime.getRuntime().exec("cmd /c start cmd.exe /c \"dir && ping localhost\"");
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    while (process.isAlive()) { }
};
英文:

You can use thread pool with fixed size.
For example:

public static void main(String[] args) throws Exception {
        ExecutorService threadPool = Executors.newFixedThreadPool(5);

        for (int i = 0; i &lt; 50; i++) {
            threadPool.submit(runTheProc);
        }

}

private static final Runnable runTheProc = () -&gt; {
        Process process;
        try {
            process = Runtime.getRuntime().exec(&quot;cmd /c start cmd.exe /c \&quot;dir &amp;&amp; ping localhost\&quot;&quot;);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        while (process.isAlive()) { }
};

huangapple
  • 本文由 发表于 2020年9月5日 22:32:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/63755047.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定