實(shí)例分析Java單線程與多線程
線程:每一個(gè)任務(wù)稱為一個(gè)線程,線程不能獨(dú)立的存在,它必須是進(jìn)程的一部分
單線程:般常見(jiàn)的Java應(yīng)用程序都是單線程的,比如運(yùn)行helloworld的程序時(shí),會(huì)啟動(dòng)jvm進(jìn)程,然后運(yùn)行main方法產(chǎn)生線程,main方法也被稱為主線程。
多線程:同時(shí)運(yùn)行一個(gè)以上線程的程序稱為多線程程序,多線程能滿足程序員編寫高效率的程序來(lái)達(dá)到充分利用 CPU 的目的。
單線程代碼例子:
public class SingleThread {
public static void main(String[] args){
Thread thread = Thread.currentThread(); //獲取當(dāng)前運(yùn)行的線程對(duì)象
thread.setName("單線程"); //線程重命名
System.out.println(thread.getName()+"正在運(yùn)行");
for(int i=0;i<10;i++){
System.out.println("線程正在休眠:"+i);
try {
thread.sleep(1000); //線程休眠,延遲一秒
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("線程出錯(cuò)");
}
}
}
}
多線程代碼例子:
注意:多線程有兩種實(shí)現(xiàn)方式,一種是繼承Thread類,另一種是實(shí)現(xiàn)Runnable接口。
繼承Thread類實(shí)現(xiàn)多線程
public class TestThread {
public static void main(String[] args){
Thread t1 = new ExtendThread("t1",1000); //使用上轉(zhuǎn)對(duì)象創(chuàng)建線程,并構(gòu)造線程名字和線程休眠時(shí)間
Thread t2 = new ExtendThread("t2",2000);
Thread t3 = new ExtendThread("t3",3000);
t1.start(); //啟動(dòng)線程并調(diào)用run方法
t2.start();
t3.start();
}
}
class ExtendThread extends Thread{ //繼承Thread的類
String name;
int time;
public ExtendThread(String name, int time) { //構(gòu)造線程名字和休眠時(shí)間
this.name=name;
this.time=time;
}
public void run(){ //重寫Thread類的run方法
try{
sleep(time); //所有線程加入休眠
}
catch(InterruptedExceptione){
e.printStackTrace();
System.out.println("線程中斷異常");
}
System.out.println("名稱為:"+name+",線程休眠:"+time+"毫秒");
}
}
實(shí)現(xiàn)Runnable接口的多線程
public class RunnableThread {
public static void main(String[] args){
Runnable r1=new ImplRunnable("r1",1000); //Runnable接口必須依托Thread類才能創(chuàng)建線程
Thread t1=new Thread(r1); //Runnable并不能調(diào)用start()方法,因?yàn)椴皇蔷€程,所以要用Thread類加入線程
Runnable r2=new ImplRunnable("r2",2000);
Thread t2=new Thread(r2);
Runnable r3=new ImplRunnable("r3",3000);
Thread t3=new Thread(r3);
t1.start(); //啟動(dòng)線程并調(diào)用run方法
t2.start();
t3.start();
}
}
class ImplRunnable implements Runnable{ //繼承Runnable接口的類
String name;
int time;
public ImplRunnable(String name, int time) { //構(gòu)造線程名字和休眠時(shí)間
this.name = name;
this.time = time;
}
@Override
public void run() { //實(shí)現(xiàn)Runnable的run方法
try{
Thread.sleep(time); //所有線程加入休眠
}
catch(InterruptedException e){
e.printStackTrace();
System.out.println("線程中斷異常");
}
System.out.println("名稱為:"+name+",線程休眠:"+time+"毫秒");
}
}
說(shuō)明:Thread類實(shí)際上也是實(shí)現(xiàn)了Runnable接口的類。
實(shí)現(xiàn)Runnable接口比繼承Thread類所具有的優(yōu)勢(shì)
相關(guān)文章
Java常見(jiàn)數(shù)據(jù)結(jié)構(gòu)面試題(帶答案)
這篇文章主要介紹了Java常見(jiàn)數(shù)據(jù)結(jié)構(gòu)面試題,帶有答案及解釋,希望對(duì)廣大的程序愛(ài)好者有所幫助,同時(shí)祝大家有一個(gè)好成績(jī),需要的朋友可以參考下。2017-09-09
Spring Boot定時(shí)任務(wù)單線程多線程實(shí)現(xiàn)代碼解析
這篇文章主要介紹了Spring Boot定時(shí)任務(wù)單線程多線程實(shí)現(xiàn)代碼解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08
Java中的服務(wù)發(fā)現(xiàn)與負(fù)載均衡及Eureka與Ribbon的應(yīng)用小結(jié)
這篇文章主要介紹了Java中的服務(wù)發(fā)現(xiàn)與負(fù)載均衡:Eureka與Ribbon的應(yīng)用,通過(guò)使用Eureka和Ribbon,我們可以在Java項(xiàng)目中實(shí)現(xiàn)高效的服務(wù)發(fā)現(xiàn)和負(fù)載均衡,需要的朋友可以參考下2024-08-08
Java編程二項(xiàng)分布的遞歸和非遞歸實(shí)現(xiàn)代碼實(shí)例
這篇文章主要介紹了Java編程二項(xiàng)分布的遞歸和非遞歸實(shí)現(xiàn)代碼實(shí)例,小編覺(jué)得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-01-01
MyBatis傳入多個(gè)參數(shù)時(shí)parameterType的寫法
這篇文章主要介紹了MyBatis傳入多個(gè)參數(shù)時(shí)parameterType的寫法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12
java方法實(shí)現(xiàn)簡(jiǎn)易ATM功能
這篇文章主要為大家詳細(xì)介紹了用java方法實(shí)現(xiàn)簡(jiǎn)易ATM功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-04-04

