java 多線程Thread與runnable的區(qū)別
java 多線程Thread與runnable的區(qū)別
java中實(shí)現(xiàn)多線程的方法有兩種:繼承Thread類和實(shí)現(xiàn)runnable接口
1,繼承Thread類,重寫父類run()方法
public class thread1 extends Thread {
public void run() {
for (int i = 0; i < 10000; i++) {
System.out.println("我是線程"+this.getId());
}
}
public static void main(String[] args) {
thread1 th1 = new thread1();
thread1 th2 = new thread1();
th1.run();
th2.run();
}
}
run()方法只是普通的方法,是順序執(zhí)行的,即th1.run()執(zhí)行完成后才執(zhí)行th2.run(),這樣寫只用一個主線程。多線程就失去了意義,所以應(yīng)該用start()方法來啟動線程,start()方法會自動調(diào)用run()方法。上述代碼改為:
public class thread1 extends Thread {
public void run() {
for (int i = 0; i < 10000; i++) {
System.out.println("我是線程"+this.getId());
}
}
public static void main(String[] args) {
thread1 th1 = new thread1();
thread1 th2 = new thread1();
th1.start();
th2.start();
}
}
通過start()方法啟動一個新的線程。這樣不管th1.start()調(diào)用的run()方法是否執(zhí)行完,都繼續(xù)執(zhí)行th2.start()如果下面有別的代碼也同樣不需要等待th2.start()執(zhí)行完成,而繼續(xù)執(zhí)行。(輸出的線程id是無規(guī)則交替輸出的)
2,實(shí)現(xiàn)runnable接口
public class thread2 implements Runnable {
public String ThreadName;
public thread2(String tName){
ThreadName = tName;
}
public void run() {
for (int i = 0; i < 10000; i++) {
System.out.println(ThreadName);
}
}
public static void main(String[] args) {
thread2 th1 = new thread2("線程A");
thread2 th2 = new thread2("Thread-B");
th1.run();
th2.run();
}
}
和Thread的run方法一樣Runnable的run只是普通方法,在main方法中th2.run()必須等待th1.run()執(zhí)行完成后才能執(zhí)行,程序只用一個線程。要多線程的目的,也要通過Thread的start()方法(runnable是沒有start方法)。上述代碼修改為:
public class thread2 implements Runnable {
public String ThreadName;
public thread2(String tName){
ThreadName = tName;
}
public void run() {
for (int i = 0; i < 10000; i++) {
System.out.println(ThreadName);
}
}
public static void main(String[] args) {
thread2 th1 = new thread2("線程A");
thread2 th2 = new thread2("Thread-B");
Thread myth1 = new Thread(th1);
Thread myth2 = new Thread(th2);
myth1.start();
myth2.start();
}
}
總結(jié):實(shí)現(xiàn)java多線程的2種方式,runable是接口,thread是類,runnable只提供一個run方法,建議使用runable實(shí)現(xiàn) java多線程,不管如何,最終都需要通過thread.start()來使線程處于可運(yùn)行狀態(tài)。
以上就是關(guān)于java多線程的實(shí)例詳解,如有疑問請留言或者到本站社區(qū)交流討論,本站關(guān)于線程的文章還有很多,希望大家搜索查閱,大家共同進(jìn)步!
相關(guān)文章
springboot整合apache ftpserver詳細(xì)教程(推薦)
這篇文章主要介紹了springboot整合apache ftpserver詳細(xì)教程,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2020-01-01
Springboot發(fā)送郵件功能的實(shí)現(xiàn)詳解
電子郵件是—種用電子手段提供信息交換的通信方式,是互聯(lián)網(wǎng)應(yīng)用最廣的服務(wù)。本文詳細(xì)為大家介紹了SpringBoot實(shí)現(xiàn)發(fā)送電子郵件功能的示例代碼,需要的可以參考一下2022-09-09
java中ThreadLocal的應(yīng)用場景實(shí)例分析
在本篇文章里小編給大家整理的是一篇關(guān)于java中ThreadLocal的應(yīng)用場景實(shí)例分析,對此有興趣的朋友們可以學(xué)習(xí)參考下。2021-02-02
Java狀態(tài)設(shè)計(jì)模式實(shí)現(xiàn)對象狀態(tài)轉(zhuǎn)換的優(yōu)雅方式
Java狀態(tài)設(shè)計(jì)模式通過將對象的行為和狀態(tài)分離,使對象能夠根據(jù)不同的狀態(tài)進(jìn)行不同的行為操作。它通過將狀態(tài)抽象成一個獨(dú)立的類來實(shí)現(xiàn)對狀態(tài)的封裝,從而簡化了復(fù)雜的條件判斷和狀態(tài)轉(zhuǎn)換2023-04-04
JAVA中關(guān)于Long類型返回前端精度丟失問題處理辦法
這篇文章主要介紹了后端JavaBean的id屬性從Long類型改為雪花算法后出現(xiàn)的精度丟失問題,解決方案包括將id字段類型改為字符串或使用Jackson序列化方式,需要的朋友可以參考下2024-11-11
淺談SpringMVC+Spring3+Hibernate4開發(fā)環(huán)境搭建
MVC已經(jīng)是現(xiàn)代Web開發(fā)中的一個很重要的部分,本文介紹一下SpringMVC+Spring3+Hibernate4的開發(fā)環(huán)境搭建,有興趣的可以了解一下。2017-01-01
Spring遠(yuǎn)程加載配置的實(shí)現(xiàn)方法詳解
這篇文章主要介紹了Spring遠(yuǎn)程加載配置的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2023-03-03

