欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

深入探究Java線程的創(chuàng)建與構(gòu)造方法

 更新時間:2022年04月26日 10:08:17   作者:淡沫初夏Zz  
這篇文章主要給大家分享的是java線程的創(chuàng)建以及構(gòu)造方法,想了解具體方式的小伙伴可以參考下面文章內(nèi)容,希望對你有所幫助

一、創(chuàng)建線程

啟動線程—start 方法

通過覆寫 run 方法創(chuàng)建?個線程對象,但線程對象被創(chuàng)建出來并不意味著線程就開始運行了

  • 覆寫run方法是給線程指令清單
  • 但是start方法,則是讓線程去真正的執(zhí)行

方法一

繼承Thread類

/**
 * 繼承Thread創(chuàng)建線程
 */
class MyThread1 extends Thread{
    @Override
    public void run() {
        //業(yè)務(wù)代碼
        Thread thread = Thread.currentThread();
        System.out.println("名稱:" + thread.getName());
    }
}
public class ThreadDemo1 {
    public static void main(String[] args) {
        //獲得當前的線程
        Thread mainThread = Thread.currentThread();
        System.out.println("名稱:" + mainThread.getName());
        Thread thread = new MyThread1();
        //開啟線程
        thread.start();
    }
}

因為 Java 是單繼承,繼承了 Thread 就不能繼承其他類了,然而 Java 可以實現(xiàn)多個接口,于是有了下?種方式

方法二

實現(xiàn)Runnable接口

/**
 * 使用Runnable接口創(chuàng)建線程
 */
class MyThread2 implements Runnable{
    @Override
    public void run() {
        Thread thread = Thread.currentThread();//得到當前線程
        System.out.println("名稱:" + thread.getName());
    }
}
public class ThreadDemo2 {
    public static void main(String[] args) {
        MyThread2 myThread2 = new MyThread2();
        //創(chuàng)建線程
        Thread thread = new Thread(myThread2);
        //啟動線程
        thread.start();
    }
}

方法三

繼承Thread類使用匿名內(nèi)部類

/**
 * 繼承Thread使用匿名內(nèi)部類創(chuàng)建
 */
public class ThreadDemo4 {
    public static void main(String[] args) {
        Thread thread = new Thread(){
            @Override
            public void run() {
//                業(yè)務(wù)代碼
                Thread thread2 = Thread.currentThread();
                System.out.println("名稱" + thread2.getName());
            }
        };
        //開始線程
        thread.start();

    }
}

方法四

實現(xiàn)Runnable接口,使用匿名內(nèi)部類

/**
 * runnable使用匿名內(nèi)部類創(chuàng)建
 */
public class ThreadDemo3 {
    public static void main(String[] args) {
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                //具體業(yè)務(wù)
                Thread thread1 = Thread.currentThread();
                System.out.println("名稱:" + thread1.getName());
            }
        });
        //開啟線程
        thread.start();
    }
}

方法五

使用lambda表達式

/**
 * 使用lambda表達式
 */
public class ThreadDemo5 {
    public static void main(String[] args) {
        //創(chuàng)建線程
        Thread thread = new Thread(()->{
            //業(yè)務(wù)代碼
            Thread thread3 = Thread.currentThread();
            System.out.println("名稱" + thread3.getName());
        });
        //啟動線程
        thread.start();
    }
}

方法六

帶返回值的 Callable

import java.util.Random;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * 使用callable創(chuàng)建線程
 */
public class ThreadDemo6 {
    public static void main(String[] args)throws ExecutionException, InterruptedException {
        // 創(chuàng)建 Callable 實例
        MyCallable callable = new MyCallable();
        // 用于接收 Callable 結(jié)果的對象
        FutureTask<Integer> futureTask = new FutureTask<Integer>(callable);
        // 創(chuàng)建新線程
        Thread thread = new Thread(futureTask);
        // 啟動線程
        thread.start();
        // 接收新線程執(zhí)行的結(jié)果
        int result = futureTask.get();
        System.out.println(Thread.currentThread().getName() +
                "——新線程返回的結(jié)果為:" + result);
    }
}
/**
 * Callable<V> 泛型里面可以是任意數(shù)據(jù)類型
 */
class MyCallable implements Callable<Integer> {
    @Override
    public Integer call() throws Exception {
        // 生成隨機數(shù):0-9
        int randomNum = new Random().nextInt(10);
        System.out.println(Thread.currentThread().getName() +
                "——隨機數(shù):" + randomNum);
        return randomNum;
    }
}

在創(chuàng)建線程時, 如果是 JDK 1.8 以上版本,在不需要獲得線程執(zhí)行結(jié)果的情況下,推薦使用Lambda 方式來創(chuàng)建線程,因為它的寫法足夠簡潔;如果想要獲取線程執(zhí)行結(jié)果,可使用FutureTask + Callable 的方式來實現(xiàn)

二、run方法和start方法的區(qū)別

run 方法和 start 方法的主要區(qū)別如下:

①方法性質(zhì)不同

run 是一個普通方法,而 start 是開啟新線程的方法。

②執(zhí)行速度不同

調(diào)用 run 方法會立即執(zhí)行任務(wù),調(diào)用 start 方法是將線程的狀態(tài)改為就緒狀態(tài),不會立即執(zhí)行。

③調(diào)用次數(shù)不同

run 方法可以被重復(fù)調(diào)用,而 start 方法只能被調(diào)用一次。start 方法之所以不能被重復(fù)調(diào)用的原因是,線程的狀態(tài)是不可逆的,Thread 在 start 的實現(xiàn)源碼中做了判斷,如果線程不是新建狀態(tài) NEW,則會拋出非法線程狀態(tài)異常IllegalThreadStateException

  public static void main(String[] args) {
        // 創(chuàng)建線程一
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                // 獲取到當前執(zhí)行的線程
                Thread currThread = Thread.currentThread();
                System.out.println("執(zhí)行線程一,線程名:" + currThread.getName());
            }
        });
        // 調(diào)用 run 方法
        thread.run();
        // 多次調(diào)用 run 方法
        thread.run();

        // 創(chuàng)建線程二
        Thread thread2 = new Thread(new Runnable() {
            @Override
            public void run() {
                // 獲取到當前執(zhí)行的線程
                Thread currThread = Thread.currentThread();
                System.out.println("執(zhí)行線程二,線程名:" + currThread.getName());
            }
        });
       // 調(diào)用 start 方法
        thread2.start();
       // 多次調(diào)用 start 方法
        thread2.start();
    }

從上述結(jié)果可以看出,run 方法多次調(diào)用可用正常執(zhí)行,而第二次調(diào)用 start 方法時程序就報錯了,提示“IllegalThreadStateException”非法線程狀態(tài)異常

總結(jié)

方法性質(zhì)不同:run 是一個普通方法,而 start 是開啟新線程的方法。

執(zhí)行速度不同:調(diào)用 run 方法會立即執(zhí)行任務(wù),調(diào)用 start 方法是將線程的狀態(tài)改為就緒狀態(tài),不會立即執(zhí)行。

調(diào)用次數(shù)不同:run 方法可以被重復(fù)調(diào)用,而 start 方法只能被調(diào)用一次。

三、線程的構(gòu)造方法

1、Thread()創(chuàng)建線程

Thread t1 = new Thread();

2、Thread(Runnable target) 創(chuàng)建線程

Thread t2 = new Thread(new MyRunnable());

3、Thread(String name)創(chuàng)建線程且命名

/**
 * 創(chuàng)建線程,構(gòu)造方法設(shè)置線程名稱
 */
public class ThreadDemo9 {
    public static void main(String[] args) {
        //構(gòu)造方法設(shè)置名稱
        Thread thread = new Thread("線程1"){
            @Override
            public void run() {
                //休眠線程
                try {
                    Thread.sleep(1000*60*60);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };
        thread.start();
    }
}

4、Thread(Runnable target, String name),用Runnable 對象創(chuàng)建線程對象,并命名

/**
 * 創(chuàng)建線程,并設(shè)置名稱
 */
public class ThreadDemo10 {
    public static void main(String[] args) {
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(1000*60*60);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"Runable-Thread");
        thread.start();
    }
}

到此這篇關(guān)于深入探究Java線程的創(chuàng)建與構(gòu)造方法的文章就介紹到這了,更多相關(guān)Java線程內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 淺談Java中的URL和URLConnection

    淺談Java中的URL和URLConnection

    這篇文章主要介紹了淺談Java中的URL和URLConnection,URL代表的是一種資源,它使用的是統(tǒng)一資源定位格式, 其實我們還可以用URL來構(gòu)造對象的,java中有大量的構(gòu)造函數(shù)允許使用一個個的分串來指定URL,需要的朋友可以參考下
    2023-08-08
  • Spring Boot Actuator監(jiān)控的簡單使用方法示例代碼詳解

    Spring Boot Actuator監(jiān)控的簡單使用方法示例代碼詳解

    這篇文章主要介紹了Spring Boot Actuator監(jiān)控的簡單使用,本文通過實例代碼圖文相結(jié)合給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-06-06
  • Maven scala和java混合打包方式

    Maven scala和java混合打包方式

    這篇文章主要介紹了Maven scala和java混合打包方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-06-06
  • 淺談Java 8 新增函數(shù)式接口到底是什么

    淺談Java 8 新增函數(shù)式接口到底是什么

    這篇文章主要介紹了淺談Java 8 新增函數(shù)式接口到底是什么,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-01-01
  • Java代碼重用之功能與上下文重用

    Java代碼重用之功能與上下文重用

    代碼重用通常使得程序開發(fā)更加快速,并使得 BUG 減少。一旦一段代碼被封裝和重用,那么只需要檢查很少的一段代碼即可確保程序的正確性。接下來通過本文給大家介紹Java代碼重用之功能與上下文重用的相關(guān)知識,感興趣的朋友跟隨腳本之家小編一起學習吧
    2018-05-05
  • 一文帶你了解Java選擇排序的原理與實現(xiàn)

    一文帶你了解Java選擇排序的原理與實現(xiàn)

    選擇排序:(Selection sort)是一種簡單直觀的排序算法,也是一種不穩(wěn)定的排序方法。本文主要為大家介紹一下選擇排序的原理與實現(xiàn),希望對大家有所幫助
    2022-11-11
  • Springboot項目出現(xiàn)java.lang.ArrayStoreException的異常分析

    Springboot項目出現(xiàn)java.lang.ArrayStoreException的異常分析

    這篇文章介紹了Springboot項目出現(xiàn)java.lang.ArrayStoreException的異常分析,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-12-12
  • Eclipse創(chuàng)建java程序可執(zhí)行jar包教程

    Eclipse創(chuàng)建java程序可執(zhí)行jar包教程

    這篇文章主要為大家分享了Eclipse創(chuàng)建java程序可執(zhí)行jar包教程,具有一定的實用性和參考價值,感興趣的小伙伴們可以參考一下
    2016-05-05
  • JAVA使用commos-fileupload實現(xiàn)文件上傳與下載實例解析

    JAVA使用commos-fileupload實現(xiàn)文件上傳與下載實例解析

    這篇文章主要介紹了JAVA使用commos-fileupload實現(xiàn)文件上傳與下載的相關(guān)資料,需要的朋友可以參考下
    2016-02-02
  • 簡述IDEA集成Git在實際項目中的運用

    簡述IDEA集成Git在實際項目中的運用

    這篇文章主要介紹了IDEA集成Git在實際項目中的運用,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-07-07

最新評論