java基本教程之常用的實(shí)現(xiàn)多線程的兩種方式 java多線程教程
關(guān)于線程池的內(nèi)容,我們以后會(huì)詳細(xì)介紹;現(xiàn)在,先對(duì)的Thread和Runnable進(jìn)行了解。本章內(nèi)容包括:
Thread和Runnable的簡(jiǎn)介
Thread和Runnable的異同點(diǎn)
Thread和Runnable的多線程的示例
Thread和Runnable簡(jiǎn)介
Runnable 是一個(gè)接口,該接口中只包含了一個(gè)run()方法。它的定義如下:
public interface Runnable {
public abstract void run();
}
Runnable的作用,實(shí)現(xiàn)多線程。我們可以定義一個(gè)類A實(shí)現(xiàn)Runnable接口;然后,通過(guò)new Thread(new A())等方式新建線程。
Thread 是一個(gè)類。Thread本身就實(shí)現(xiàn)了Runnable接口。它的聲明如下:
public class Thread implements Runnable {}
Thread的作用,實(shí)現(xiàn)多線程。
Thread和Runnable的異同點(diǎn)
Thread 和 Runnable 的相同點(diǎn):都是“多線程的實(shí)現(xiàn)方式”。
Thread 和 Runnable 的不同點(diǎn):
Thread 是類,而Runnable是接口;Thread本身是實(shí)現(xiàn)了Runnable接口的類。我們知道“一個(gè)類只能有一個(gè)父類,但是卻能實(shí)現(xiàn)多個(gè)接口”,因此Runnable具有更好的擴(kuò)展性。
此外,Runnable還可以用于“資源的共享”。即,多個(gè)線程都是基于某一個(gè)Runnable對(duì)象建立的,它們會(huì)共享Runnable對(duì)象上的資源。
通常,建議通過(guò)“Runnable”實(shí)現(xiàn)多線程!
Thread和Runnable的多線程示例
1. Thread的多線程示例
下面通過(guò)示例更好的理解Thread和Runnable,借鑒網(wǎng)上一個(gè)例子比較具有說(shuō)服性的例子。
// ThreadTest.java 源碼
class MyThread extends Thread{
private int ticket=10;
public void run(){
for(int i=0;i<20;i++){
if(this.ticket>0){
System.out.println(this.getName()+" 賣票:ticket"+this.ticket--);
}
}
}
};
public class ThreadTest {
public static void main(String[] args) {
// 啟動(dòng)3個(gè)線程t1,t2,t3;每個(gè)線程各賣10張票!
MyThread t1=new MyThread();
MyThread t2=new MyThread();
MyThread t3=new MyThread();
t1.start();
t2.start();
t3.start();
}
}
運(yùn)行結(jié)果:
Thread-0 賣票:ticket10
Thread-1 賣票:ticket10
Thread-2 賣票:ticket10
Thread-1 賣票:ticket9
Thread-0 賣票:ticket9
Thread-1 賣票:ticket8
Thread-2 賣票:ticket9
Thread-1 賣票:ticket7
Thread-0 賣票:ticket8
Thread-1 賣票:ticket6
Thread-2 賣票:ticket8
Thread-1 賣票:ticket5
Thread-0 賣票:ticket7
Thread-1 賣票:ticket4
Thread-2 賣票:ticket7
Thread-1 賣票:ticket3
Thread-0 賣票:ticket6
Thread-1 賣票:ticket2
Thread-2 賣票:ticket6
結(jié)果說(shuō)明:
(01) MyThread繼承于Thread,它是自定義個(gè)線程。每個(gè)MyThread都會(huì)賣出10張票。
(02) 主線程main創(chuàng)建并啟動(dòng)3個(gè)MyThread子線程。每個(gè)子線程都各自賣出了10張票。
2. Runnable的多線程示例
下面,我們對(duì)上面的程序進(jìn)行修改。通過(guò)Runnable實(shí)現(xiàn)一個(gè)接口,從而實(shí)現(xiàn)多線程。
// RunnableTest.java 源碼
class MyThread implements Runnable{
private int ticket=10;
public void run(){
for(int i=0;i<20;i++){
if(this.ticket>0){
System.out.println(Thread.currentThread().getName()+" 賣票:ticket"+this.ticket--);
}
}
}
};
public class RunnableTest {
public static void main(String[] args) {
MyThread mt=new MyThread();
// 啟動(dòng)3個(gè)線程t1,t2,t3(它們共用一個(gè)Runnable對(duì)象),這3個(gè)線程一共賣10張票!
Thread t1=new Thread(mt);
Thread t2=new Thread(mt);
Thread t3=new Thread(mt);
t1.start();
t2.start();
t3.start();
}
}
運(yùn)行結(jié)果:
Thread-0 賣票:ticket10
Thread-2 賣票:ticket8
Thread-1 賣票:ticket9
Thread-2 賣票:ticket6
Thread-0 賣票:ticket7
Thread-2 賣票:ticket4
Thread-1 賣票:ticket5
Thread-2 賣票:ticket2
Thread-0 賣票:ticket3
Thread-1 賣票:ticket1
結(jié)果說(shuō)明:
(01) 和上面“MyThread繼承于Thread”不同;這里的MyThread實(shí)現(xiàn)了Thread接口。
(02) 主線程main創(chuàng)建并啟動(dòng)3個(gè)子線程,而且這3個(gè)子線程都是基于“mt這個(gè)Runnable對(duì)象”而創(chuàng)建的。運(yùn)行結(jié)果是這3個(gè)子線程一共賣出了10張票。這說(shuō)明它們是共享了MyThread接口的。
- Java多線程中的單例模式兩種實(shí)現(xiàn)方式
- Java多線程實(shí)現(xiàn)的兩種方式
- 詳細(xì)解讀JAVA多線程實(shí)現(xiàn)的三種方式
- java實(shí)現(xiàn)多線程的兩種方式繼承Thread類和實(shí)現(xiàn)Runnable接口的方法
- 總結(jié)Java中線程的狀態(tài)及多線程的實(shí)現(xiàn)方式
- 詳解Java實(shí)現(xiàn)多線程的三種方式
- Java 實(shí)現(xiàn)多線程的幾種方式匯總
- Java多線程的實(shí)現(xiàn)方式比較(兩種方式比較)
- 詳解三種java實(shí)現(xiàn)多線程的方式
- 淺談Java的兩種多線程實(shí)現(xiàn)方式
相關(guān)文章
mybatis-generator自動(dòng)生成dao、mapping、bean配置操作
這篇文章主要介紹了mybatis-generator自動(dòng)生成dao、mapping、bean配置操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-08-08基于FlashPaper實(shí)現(xiàn)JSP在線閱讀代碼示例
這篇文章主要介紹了基于FlashPaper實(shí)現(xiàn)JSP在線閱讀代碼示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-10-10SPFA算法的實(shí)現(xiàn)原理及其應(yīng)用詳解
SPFA算法,全稱為Shortest?Path?Faster?Algorithm,是求解單源最短路徑問(wèn)題的一種常用算法,本文就來(lái)聊聊它的實(shí)現(xiàn)原理與簡(jiǎn)單應(yīng)用吧2023-05-05Simple Java Mail郵件發(fā)送實(shí)現(xiàn)過(guò)程解析
這篇文章主要介紹了Simple Java Mail郵件發(fā)送實(shí)現(xiàn)過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-11-11詳解Java數(shù)據(jù)庫(kù)連接JDBC基礎(chǔ)知識(shí)(操作數(shù)據(jù)庫(kù):增刪改查)
這篇文章主要介紹了詳解Java數(shù)據(jù)庫(kù)連接JDBC基礎(chǔ)知識(shí)(操作數(shù)據(jù)庫(kù):增刪改查),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01