溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

如何實(shí)現(xiàn)多線程-線程的創(chuàng)建

發(fā)布時(shí)間:2020-07-20 11:13:19 來源:億速云 閱讀:134 作者:Leah 欄目:編程語言

今天就跟大家聊聊有關(guān)如何實(shí)現(xiàn)多線程-線程的創(chuàng)建,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

                                                           線程的創(chuàng)建方式

總結(jié)一下多線程的創(chuàng)建方式,多線程的實(shí)現(xiàn)一共四種方法,接下來將詳談一下創(chuàng)建的方式

1、繼承Thread類,而后覆寫run()方法

2、實(shí)現(xiàn)Runnable接口,而后覆寫run()方法

3、實(shí)現(xiàn)callable接口,而后覆寫call<v>方法

4、線程池(后面專門說,因?yàn)檩^復(fù)雜)

注意:無論使用哪種方式創(chuàng)建線程,啟動線程一律使用Thread類提供的start()方法。

1.繼承Thread類覆寫run方法

class MyThread extends Thread {
    private String title;
    private int ticket = 20;
 
    public MyThread(String title) {
        this.title = title;
    }
 
    public void run() {  //放每個(gè)線程的子任務(wù)
        while (ticket > 0) {
            System.out.println("當(dāng)前線程為"+title+",還剩下"+ticket--+"票");
        }
    }
}
 
public class ThreadTest {
    public static void main(String[] args) {
        MyThread myThread1 = new MyThread("黃牛A");
        MyThread myThread2 = new MyThread("黃牛B");
        MyThread myThread3 = new MyThread("黃牛C");
        myThread1.start();
        myThread2.start();
        myThread3.start();
    }
}

2.實(shí)現(xiàn)Runnable接口覆寫run方法

class MyRunnable implements Runnable{
    @Override
    public void run() {
      for(int i =0;i <10;i++){
          System.out.println(Thread.currentThread().getName()+"、i="+i);
      }
    }
}
public class RunnableTest {
    public static void main(String[] args) {
       Runnable runnable =new MyRunnable();      //向上轉(zhuǎn)型
        new Thread(runnable,"線程A").start();    //設(shè)置線程名字
        new Thread(runnable).start();     //沒有設(shè)置線程名字,則是系統(tǒng)默認(rèn)從 Thread-(0,1,2...)
        Thread thread1 = new Thread(runnable);
        thread1.setName("線程B");        //調(diào)用setName()設(shè)置名字
        thread1.start();
    }
}

這里呢順便介紹了線程名字的創(chuàng)建3種:

(1)在括號后直接加名字

(2)調(diào)用setName()設(shè)置名字

(3)不設(shè)置名字,則是系統(tǒng)默認(rèn)從Thread-(0,1,2....)

3.實(shí)現(xiàn)Callable接口覆寫call<V>方法

class MyCallable implements Callable<String>{
    private int ticket =20;
    @Override
    public String call() throws Exception {
        while(ticket > 0){
            System.out.println(Thread.currentThread().getName()+"還剩下"+ticket--+"票");
        }
        return "票賣完了,再見";
    }
}
public class CallableTest {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //產(chǎn)生Callable對象
        MyCallable myCallable = new MyCallable();
        //產(chǎn)生FutureTask對象
        FutureTask futureTask = new FutureTask(myCallable);
        Thread thread = new Thread(futureTask);
        thread.start();
        System.out.println(futureTask.get()); //接收Callable對象的返回值
    }
}

1.先產(chǎn)生Callable對象

2.產(chǎn)生FutureTask對象

3.創(chuàng)建Thread傳入FutureTask對象

4. 接收Callable接口的返回值是Future中g(shù)et()方法

三個(gè)創(chuàng)建線程的方式比較

1.繼承Thread類有單繼承局限,相對而言實(shí)現(xiàn)Runnable接口更加靈活,并且Thread類本身也實(shí)現(xiàn)了Runnable接口輔助真實(shí)線程類

2.實(shí)現(xiàn)Runnable接口可以更好的實(shí)現(xiàn)程序共享的概念

3.Callable接口就是需要有返回值時(shí)用到

看完上述內(nèi)容,你們對如何實(shí)現(xiàn)多線程-線程的創(chuàng)建有進(jìn)一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI