Java并發(fā)編程之創(chuàng)建線程
先講述一下Java中的應(yīng)用程序和進(jìn)程相關(guān)的概念知識(shí),然后再闡述如何創(chuàng)建線程以及如何創(chuàng)建進(jìn)程。下面是本文的目錄大綱:
一.Java中關(guān)于應(yīng)用程序和進(jìn)程相關(guān)的概念
二.Java中如何創(chuàng)建線程
三.Java中如何創(chuàng)建進(jìn)程
一.Java中關(guān)于應(yīng)用程序和進(jìn)程相關(guān)的概念
在Java中,一個(gè)應(yīng)用程序?qū)?yīng)著一個(gè)JVM實(shí)例(也有地方稱為JVM進(jìn)程),一般來說名字默認(rèn)為java.exe或者javaw.exe(windows下可以通過任務(wù)管理器查看)。Java采用的是單線程編程模型,即在我們自己的程序中如果沒有主動(dòng)創(chuàng)建線程的話,只會(huì)創(chuàng)建一個(gè)線程,通常稱為主線程。但是要注意,雖然只有一個(gè)線程來執(zhí)行任務(wù),不代表JVM中只有一個(gè)線程,JVM實(shí)例在創(chuàng)建的時(shí)候,同時(shí)會(huì)創(chuàng)建很多其他的線程(比如垃圾收集器線程)。
由于Java采用的是單線程編程模型,因此在進(jìn)行UI編程時(shí)要注意將耗時(shí)的操作放在子線程中進(jìn)行,以避免阻塞主線程(在UI編程時(shí),主線程即UI線程,用來處理用戶的交互事件)。
二.Java中如何創(chuàng)建線程
在java中如果要?jiǎng)?chuàng)建線程的話,一般有兩種方式:1)繼承Thread類;2)實(shí)現(xiàn)Runnable接口。
1.繼承Thread類
繼承Thread類的話,必須重寫run方法,在run方法中定義需要執(zhí)行的任務(wù)。
class MyThread extends Thread{ private static int num = 0; public MyThread(){ num++; } @Override public void run() { System.out.println("主動(dòng)創(chuàng)建的第"+num+"個(gè)線程"); } }
創(chuàng)建好了自己的線程類之后,就可以創(chuàng)建線程對(duì)象了,然后通過start()方法去啟動(dòng)線程。注意,不是調(diào)用run()方法啟動(dòng)線程,run方法中只是定義需要執(zhí)行的任務(wù),如果調(diào)用run方法,即相當(dāng)于在主線程中執(zhí)行run方法,跟普通的方法調(diào)用沒有任何區(qū)別,此時(shí)并不會(huì)創(chuàng)建一個(gè)新的線程來執(zhí)行定義的任務(wù)。
public class Test { public static void main(String[] args) { MyThread thread = new MyThread(); thread.start(); } } class MyThread extends Thread{ private static int num = 0; public MyThread(){ num++; } @Override public void run() { System.out.println("主動(dòng)創(chuàng)建的第"+num+"個(gè)線程"); } }
在上面代碼中,通過調(diào)用start()方法,就會(huì)創(chuàng)建一個(gè)新的線程了。為了分清start()方法調(diào)用和run()方法調(diào)用的區(qū)別,請(qǐng)看下面一個(gè)例子:
public class Test { public static void main(String[] args) { System.out.println("主線程ID:"+Thread.currentThread().getId()); MyThread thread1 = new MyThread("thread1"); thread1.start(); MyThread thread2 = new MyThread("thread2"); thread2.run(); } } class MyThread extends Thread{ private String name; public MyThread(String name){ this.name = name; } @Override public void run() { System.out.println("name:"+name+" 子線程ID:"+Thread.currentThread().getId()); } }
運(yùn)行結(jié)果:
從輸出結(jié)果可以得出以下結(jié)論:
1)thread1和thread2的線程ID不同,thread2和主線程ID相同,說明通過run方法調(diào)用并不會(huì)創(chuàng)建新的線程,而是在主線程中直接運(yùn)行run方法,跟普通的方法調(diào)用沒有任何區(qū)別;
2)雖然thread1的start方法調(diào)用在thread2的run方法前面調(diào)用,但是先輸出的是thread2的run方法調(diào)用的相關(guān)信息,說明新線程創(chuàng)建的過程不會(huì)阻塞主線程的后續(xù)執(zhí)行。
2.實(shí)現(xiàn)Runnable接口
在Java中創(chuàng)建線程除了繼承Thread類之外,還可以通過實(shí)現(xiàn)Runnable接口來實(shí)現(xiàn)類似的功能。實(shí)現(xiàn)Runnable接口必須重寫其run方法。
下面是一個(gè)例子:
public class Test { public static void main(String[] args) { System.out.println("主線程ID:"+Thread.currentThread().getId()); MyRunnable runnable = new MyRunnable(); Thread thread = new Thread(runnable); thread.start(); } } class MyRunnable implements Runnable{ public MyRunnable() { } @Override public void run() { System.out.println("子線程ID:"+Thread.currentThread().getId()); } }
Runnable的中文意思是“任務(wù)”,顧名思義,通過實(shí)現(xiàn)Runnable接口,我們定義了一個(gè)子任務(wù),然后將子任務(wù)交由Thread去執(zhí)行。注意,這種方式必須將Runnable作為Thread類的參數(shù),然后通過Thread的start方法來創(chuàng)建一個(gè)新線程來執(zhí)行該子任務(wù)。如果調(diào)用Runnable的run方法的話,是不會(huì)創(chuàng)建新線程的,這根普通的方法調(diào)用沒有任何區(qū)別。
事實(shí)上,查看Thread類的實(shí)現(xiàn)源代碼會(huì)發(fā)現(xiàn)Thread類是實(shí)現(xiàn)了Runnable接口的。
在Java中,這2種方式都可以用來創(chuàng)建線程去執(zhí)行子任務(wù),具體選擇哪一種方式要看自己的需求。直接繼承Thread類的話,可能比實(shí)現(xiàn)Runnable接口看起來更加簡潔,但是由于Java只允許單繼承,所以如果自定義類需要繼承其他類,則只能選擇實(shí)現(xiàn)Runnable接口。
三.Java中如何創(chuàng)建進(jìn)程
在Java中,可以通過兩種方式來創(chuàng)建進(jìn)程,總共涉及到5個(gè)主要的類。
第一種方式是通過Runtime.exec()方法來創(chuàng)建一個(gè)進(jìn)程,第二種方法是通過ProcessBuilder的start方法來創(chuàng)建進(jìn)程。下面就來講一講這2種方式的區(qū)別和聯(lián)系。
首先要講的是Process類,Process類是一個(gè)抽象類,在它里面主要有幾個(gè)抽象的方法,這個(gè)可以通過查看Process類的源代碼得知:
位于java.lang.Process路徑下:
public class Test { public static void main(String[] args) { System.out.println("主線程ID:"+Thread.currentThread().getId()); MyRunnable runnable = new MyRunnable(); Thread thread = new Thread(runnable); thread.start(); } } class MyRunnable implements Runnable{ public MyRunnable() { } @Override public void run() { System.out.println("子線程ID:"+Thread.currentThread().getId()); } }
1)通過ProcessBuilder創(chuàng)建進(jìn)程
ProcessBuilder是一個(gè)final類,它有兩個(gè)構(gòu)造器:
public final class ProcessBuilder { private List<String> command; private File directory; private Map<String,String> environment; private boolean redirectErrorStream; public ProcessBuilder(List<String> command) { if (command == null) throw new NullPointerException(); this.command = command; } public ProcessBuilder(String... command) { this.command = new ArrayList<String>(command.length); for (String arg : command) this.command.add(arg); } .... }
構(gòu)造器中傳遞的是需要?jiǎng)?chuàng)建的進(jìn)程的命令參數(shù),第一個(gè)構(gòu)造器是將命令參數(shù)放進(jìn)List當(dāng)中傳進(jìn)去,第二構(gòu)造器是以不定長字符串的形式傳進(jìn)去。
那么我們接著往下看,前面提到是通過ProcessBuilder的start方法來創(chuàng)建一個(gè)新進(jìn)程的,我們看一下start方法中具體做了哪些事情。下面是start方法的具體實(shí)現(xiàn)源代碼:
public Process start() throws IOException { // Must convert to array first -- a malicious user-supplied // list might try to circumvent the security check. String[] cmdarray = command.toArray(new String[command.size()]); for (String arg : cmdarray) if (arg == null) throw new NullPointerException(); // Throws IndexOutOfBoundsException if command is empty String prog = cmdarray[0]; SecurityManager security = System.getSecurityManager(); if (security != null) security.checkExec(prog); String dir = directory == null ? null : directory.toString(); try { return ProcessImpl.start(cmdarray, environment, dir, redirectErrorStream); } catch (IOException e) { // It's much easier for us to create a high-quality error // message than the low-level C code which found the problem. throw new IOException( "Cannot run program \"" + prog + "\"" + (dir == null ? "" : " (in directory \"" + dir + "\")") + ": " + e.getMessage(), e); } }
該方法返回一個(gè)Process對(duì)象,該方法的前面部分相當(dāng)于是根據(jù)命令參數(shù)以及設(shè)置的工作目錄進(jìn)行一些參數(shù)設(shè)定,最重要的是try語句塊里面的一句:
return ProcessImpl.start(cmdarray, environment, dir, redirectErrorStream);
說明真正創(chuàng)建進(jìn)程的是這一句,注意調(diào)用的是ProcessImpl類的start方法,此處可以知道start必然是一個(gè)靜態(tài)方法。那么ProcessImpl又是什么類呢?該類同樣位于java.lang.ProcessImpl路徑下,看一下該類的具體實(shí)現(xiàn):
ProcessImpl也是一個(gè)final類,它繼承了Process類:
final class ProcessImpl extends Process { // System-dependent portion of ProcessBuilder.start() static Process start(String cmdarray[], java.util.Map<String,String> environment, String dir, boolean redirectErrorStream) throws IOException { String envblock = ProcessEnvironment.toEnvironmentBlock(environment); return new ProcessImpl(cmdarray, envblock, dir, redirectErrorStream); } .... }
這是ProcessImpl類的start方法的具體實(shí)現(xiàn),而事實(shí)上start方法中是通過這句來創(chuàng)建一個(gè)ProcessImpl對(duì)象的:
return new ProcessImpl(cmdarray, envblock, dir, redirectErrorStream);
而在ProcessImpl中對(duì)Process類中的幾個(gè)抽象方法進(jìn)行了具體實(shí)現(xiàn)。
說明事實(shí)上通過ProcessBuilder的start方法創(chuàng)建的是一個(gè)ProcessImpl對(duì)象。
下面看一下具體使用ProcessBuilder創(chuàng)建進(jìn)程的例子,比如我要通過ProcessBuilder來啟動(dòng)一個(gè)進(jìn)程打開cmd,并獲取ip地址信息,那么可以這么寫:
public class Test { public static void main(String[] args) throws IOException { ProcessBuilder pb = new ProcessBuilder("cmd","/c","ipconfig/all"); Process process = pb.start(); Scanner scanner = new Scanner(process.getInputStream()); while(scanner.hasNextLine()){ System.out.println(scanner.nextLine()); } scanner.close(); } }
第一步是最關(guān)鍵的,就是將命令字符串傳給ProcessBuilder的構(gòu)造器,一般來說,是把字符串中的每個(gè)獨(dú)立的命令作為一個(gè)單獨(dú)的參數(shù),不過也可以按照順序放入List中傳進(jìn)去。
至于其他很多具體的用法不在此進(jìn)行贅述,比如通過ProcessBuilder的environment方法和directory(File directory)設(shè)置進(jìn)程的環(huán)境變量以及工作目錄等,感興趣的朋友可以查看相關(guān)API文檔。
2)通過Runtime的exec方法來創(chuàng)建進(jìn)程
首先還是來看一下Runtime類和exec方法的具體實(shí)現(xiàn),Runtime,顧名思義,即運(yùn)行時(shí),表示當(dāng)前進(jìn)程所在的虛擬機(jī)實(shí)例。
由于任何進(jìn)程只會(huì)運(yùn)行于一個(gè)虛擬機(jī)實(shí)例當(dāng)中,所以在Runtime中采用了單例模式,即只會(huì)產(chǎn)生一個(gè)虛擬機(jī)實(shí)例:
public class Runtime { private static Runtime currentRuntime = new Runtime(); /** * Returns the runtime object associated with the current Java application. * Most of the methods of class <code>Runtime</code> are instance * methods and must be invoked with respect to the current runtime object. * * @return the <code>Runtime</code> object associated with the current * Java application. */ public static Runtime getRuntime() { return currentRuntime; } /** Don't let anyone else instantiate this class */ private Runtime() {} ... }
從這里可以看出,由于Runtime類的構(gòu)造器是private的,所以只有通過getRuntime去獲取Runtime的實(shí)例。接下來著重看一下exec方法 實(shí)現(xiàn),在Runtime中有多個(gè)exec的不同重載實(shí)現(xiàn),但真正最后執(zhí)行的是這個(gè)版本的exec方法:
public Process exec(String[] cmdarray, String[] envp, File dir) throws IOException { return new ProcessBuilder(cmdarray) .environment(envp) .directory(dir) .start(); }
可以發(fā)現(xiàn),事實(shí)上通過Runtime類的exec創(chuàng)建進(jìn)程的話,最終還是通過ProcessBuilder類的start方法來創(chuàng)建的。
下面看一個(gè)例子,看一下通過Runtime的exec如何創(chuàng)建進(jìn)程,還是前面的例子,調(diào)用cmd,獲取ip地址信息:
public class Test { public static void main(String[] args) throws IOException { String cmd = "cmd "+"/c "+"ipconfig/all"; Process process = Runtime.getRuntime().exec(cmd); Scanner scanner = new Scanner(process.getInputStream()); while(scanner.hasNextLine()){ System.out.println(scanner.nextLine()); } scanner.close(); } }
要注意的是,exec方法不支持不定長參數(shù)(ProcessBuilder是支持不定長參數(shù)的),所以必須先把命令參數(shù)拼接好再傳進(jìn)去。
關(guān)于在Java中如何創(chuàng)建線程和進(jìn)程的話,暫時(shí)就講這么多了,感興趣的朋友可以參考相關(guān)資料。
相關(guān)文章
springboot整合日志處理Logback的實(shí)現(xiàn)示例
Logback是由log4j創(chuàng)始人設(shè)計(jì)的又一個(gè)開源日志組件,本文主要介紹了springboot整合日志處理Logback,文中通過示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-01-01Java全面細(xì)致講解Cookie與Session及kaptcha驗(yàn)證碼的使用
web開發(fā)階段我們主要是瀏覽器和服務(wù)器之間來進(jìn)行交互。瀏覽器和服務(wù)器之間的交互就像人和人之間進(jìn)行交流一樣,但是對(duì)于機(jī)器來說,在一次請(qǐng)求之間只是會(huì)攜帶著本次請(qǐng)求的數(shù)據(jù)的,但是可能多次請(qǐng)求之間是會(huì)有聯(lián)系的,所以提供了會(huì)話機(jī)制2022-06-06SpringBoot使用Validation進(jìn)行參數(shù)校驗(yàn)的示例詳解
在 SpringBoot項(xiàng)目開發(fā)中,有一個(gè)觀點(diǎn)是不要相信前端傳入的參數(shù),因?yàn)槟悴恢烙脩羰窃趺床僮魑覀兘涌诘模栽诤蠖艘残枰獙?duì)參數(shù)進(jìn)行校驗(yàn),這篇文章主要講講我們項(xiàng)目中最常使用的驗(yàn)證方案2023-05-05基于eclipse-temurin鏡像部署spring boot應(yīng)用的實(shí)現(xiàn)示例
本文提供了基于eclipse-temurin鏡像部署Spring Boot應(yīng)用的詳細(xì)實(shí)現(xiàn)示例,通過使用Docker鏡像,可以輕松地創(chuàng)建和管理Spring Boot應(yīng)用程序的容器化環(huán)境,感興趣的可以了解一下2023-08-08mysql數(shù)據(jù)庫忘記密碼時(shí)如何修改
本文主要介紹了mysql數(shù)據(jù)庫忘記密碼時(shí)如何修改的步驟方法,具有很好的參考價(jià)值,下面跟著小編一起來看下吧2017-02-02java 線程之對(duì)象的同步和異步(實(shí)例講解)
下面小編就為大家?guī)硪黄猨ava 線程之對(duì)象的同步和異步(實(shí)例講解)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-07-07java代碼規(guī)范之不合理命名與重復(fù)代碼示例詳解
這篇文章主要為大家介紹了java代碼規(guī)范之不合理命名與重復(fù)代碼示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09Spring Data JPA使用JPQL與原生SQL進(jìn)行查詢的操作
這篇文章主要介紹了Spring Data JPA使用JPQL與原生SQL進(jìn)行查詢的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06