Android ProcessBuilder與Runtime.getRuntime().exec分別建立進程的區別

來源:互聯網
上載者:User

標籤:android   進程   process   

在Android中想要進行Ping,在不Root機器的情況下似乎還只能進行底層命調用才能實現。

因為在Java中要進行ICMP包發送需要Root許可權。


於是只能通過建立進程來解決了,建立進程在Java中有兩種方式,分別為:

1. 調用ProcessBuilder的建構函式後執行start()
2. 用Runtime.getRuntime().exec()方法執行


經過使用後發現兩者有區別但是也並不是很大,兩個例子說明:


1.調用ProcessBuilder的建構函式後執行start():

Process process = new ProcessBuilder("/system/bin/ping").redirectErrorStream(true).start();OutputStream stdout = process.getOutputStream();InputStream stdin = process.getInputStream();BufferedReader reader = new BufferedReader(new InputStreamReader(stdin));BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(stdout));


2.用Runtime.getRuntime().exec()方法執行:

Process process = Runtime.getRuntime().exec("/system/bin/ping");OutputStream stdout = process.getOutputStream();InputStream stderr = process.getErrorStream();InputStream stdin = process.getInputStream();BufferedReader reader = new BufferedReader(new InputStreamReader(stdin));BufferedReader err= new BufferedReader(new InputStreamReader(stderr));BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(stdout));

兩者在執行效率上沒啥區別,可能是我沒有發現。兩種測試的區別在於是否可以重新導向錯誤流。

使用ProcessBuilder,可以通過redirectErrorStream(true)將錯誤輸出資料流轉移到標準輸出資料流中,這樣使用一次process.getInputStreamReader()就能讀出該進程的所有輸出。

而使用Runtime.getRuntime().exec()方法時,錯誤的輸出資料流還需通過process.getErrorStream()來獲得。


分享一個自己集合的一個進程執行後銷毀的類:

import java.io.InputStream;import java.io.OutputStream;public class ProcessModel {    /**     * 通過Android底層實現進程關閉     *     * @param process     */    public static void killProcess(Process process) {        int pid = getProcessId(process.toString());        if (pid != 0) {            try {                android.os.Process.killProcess(pid);            } catch (Exception e) {                try {                    process.destroy();                } catch (Exception ex) {                }            }        }    }    /**     * 擷取當前進程的ID     *     * @param str     * @return     */    public static int getProcessId(String str) {        try {            int i = str.indexOf("=") + 1;            int j = str.indexOf("]");            String cStr = str.substring(i, j).trim();            return Integer.parseInt(cStr);        } catch (Exception e) {            return 0;        }    }    /**     * 關閉進程的所有流     *     * @param process     */    public static void closeAllStream(Process process) {        try {            InputStream in = process.getInputStream();            if (in != null)                in.close();        } catch (Exception e) {            e.printStackTrace();        }        try {            InputStream in = process.getErrorStream();            if (in != null)                in.close();        } catch (Exception e) {            e.printStackTrace();        }        try {            OutputStream out = process.getOutputStream();            if (out != null)                out.close();        } catch (Exception e) {            e.printStackTrace();        }    }    /**     * 銷毀一個進程     *     * @param process     */    public static void processDestroy(Process process) {        if (process != null) {            try {                if (process.exitValue() != 0) {                    closeAllStream(process);                    killProcess(process);                }            } catch (IllegalThreadStateException e) {                closeAllStream(process);                killProcess(process);            }        }    }    /**     * 通過線程進行非同步銷毀     *     * @param process     */    public static void asyncProcessDestroy(final Process process) {        Thread thread = new Thread(new Runnable() {            @Override            public void run() {                processDestroy(process);            }        });        thread.setDaemon(true);        thread.start();    }}



奇怪的是,當使用線程進行大量的進程建立,最後達到一定數量(大約為1000個左右)的時候將會出現無法建立進程的情況;

此情況我不知怎麼解決,自己想的是弄一個線程池裡邊放20個已經建立的進程,而外部的線程重複利用以及建立的進程,不知這樣是否可行?

望大家探討一下解決方案,謝謝了。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.