標籤:cep begin i++ lis b2c uil ret 迴圈 平台
一、進程
抽象類別Process用來封裝進程,即執行的程式。Process主要用作由Runtime的exec方法建立的物件類型或由ProcessBuilder的start方法建立的物件類型的超類。
二、運行時環境
Runtime封裝了運行時環境,不能執行個體化Runtime對象,而是通過調用靜態方法Runtime.getRuntime方法來獲得當前Runtime對象的引用。一旦獲得當前Runtime對象的引用,就可以調用一些控制Java虛擬機器的狀態和行為。
三、記憶體管理
儘管Java提供了自動的記憶體回收功能,但是有時需要瞭解對象堆的大小或剩餘空間的大小,可以使用totalMemory方法和freeMemory方法。我們直到Java記憶體回收行程周期性地運行,回收不再使用的對象。但是,有時會希望在記憶體回收行程下次指定運行之前回收廢棄的對象。可以調用gc方法來要求運行記憶體回收行程。
class Solution { public static void main(String[] args) { Runtime runtime = Runtime.getRuntime(); Double[] arr = new Double[100000]; for (int i = 0; i < arr.length; i++) arr[i] = 0.0; arr = null; System.out.println(runtime.freeMemory()); runtime.gc(); System.out.println(runtime.freeMemory()); }}View Code
四、執行其他程式
在安全環境中,可以在多任務作業系統下使用Java執行其他重量級的進程。有幾種形式的exec方法運行命名希望啟動並執行程式,並允許提供輸入參數。exec方法返回Process對象,然後可以使用該對象控制Java程式與該新啟動並執行進程的互動方式。因為Java程式可以運行各種平台和作業系統上,所以exec時環境獨立的。
import java.io.IOException;class Solution { public static void main(String[] args) { Runtime runtime = Runtime.getRuntime(); Process process = null; try { process = runtime.exec("notepad");//運行記事本 process.waitFor();//等待進程關閉 } catch (IOException exc) { System.out.println("Cannot execute notepad"); } catch (InterruptedException exc) { System.out.println("Process interrupted"); } System.out.println(process.exitValue()); }}View Code
五、進程建立類
import java.io.IOException;class Solution { public static void main(String[] args) { try { ProcessBuilder builder = new ProcessBuilder("notepad.exe", "file.txt"); builder.start(); } catch (IOException exc) { System.out.println("Cannot execute notepad"); } }}View Code
六、系統類別
通過currentTimeMillis方法可擷取自1970年1月1日午夜到目前時間的毫秒數。
class Solution { public static void main(String[] args) { long begin = System.currentTimeMillis(); try { for (int i = 0; i < 10; i++) { Thread.sleep(1000); long end = System.currentTimeMillis(); System.out.println(end - begin); } } catch (InterruptedException exc) { System.out.println("Thread interrupted"); } }}View Code
複製數組,相較於普通迴圈更快。
class Solution { static int[] copyArray(int[] arr) { int[] cpy = new int[arr.length]; System.arraycopy(arr, 0, cpy, 0, arr.length); return cpy; } public static void main(String[] args) { int[] arr = {1, 2, 3, 4, 5}; int[] cpy = copyArray(arr); for (int i : cpy) System.out.println(i); }}View Code
環境屬性。
class Solution { public static void main(String[] args) { System.out.println(System.getProperties()); }}View Code
Java筆記:運行時