日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > java >内容正文

java

实现Java多线程

發(fā)布時(shí)間:2024/10/14 java 90 豆豆
生活随笔 收集整理的這篇文章主要介紹了 实现Java多线程 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

1. 實(shí)現(xiàn)Java多線程

有三種使用線程的方法:

  • 實(shí)現(xiàn) Runnable 接口;
  • 實(shí)現(xiàn) Callable 接口;
  • 繼承 Thread 類。

實(shí)現(xiàn) Runnable 和 Callable 接口的類只能當(dāng)做一個(gè)可以在線程中運(yùn)行的任務(wù),不是真正意義上的線程,因此最后還需要通過 Thread 來調(diào)用。可以說任務(wù)是通過線程驅(qū)動(dòng)從而執(zhí)行的。

1.1 繼承Thread類,重寫run()方法;

class MyThread extends Thread{public void run(){System.out.println("Thread body");} }public class Test{public static void main(String[] args){MyThread thread=new MyThread();thread.start();} }

1.2 實(shí)現(xiàn)Runnable接口,并實(shí)現(xiàn)該接口的run()方法;

推薦這個(gè)!!(實(shí)現(xiàn)這個(gè)接口的類還可以繼承自其它的類,用3.1就沒有辦法再去繼承別的類)

Step1:自定義類并實(shí)現(xiàn)Rubbale接口,實(shí)現(xiàn)run()方法;

Step2:創(chuàng)建Thread對(duì)象,用實(shí)現(xiàn)Runnable接口的對(duì)象作為參數(shù)實(shí)例化Thread對(duì)象;

Step3:調(diào)用Thread的start()方法。

class MyThread implements Runnable{public void run(){System.out.println("Thread body");} }public class Test{pulic static void main(String[] args){MyThread thread=new MyThread();Thread t=new Thread(thread);t.start();} }

1.3 實(shí)現(xiàn)Callable接口,重寫call()方法。

Callable接口實(shí)際上是屬于Executor框架中的功能類.

Callable接口與Runnable接口的功能類似,提供了比Runnable更強(qiáng)大的功能,主要表現(xiàn)有三點(diǎn):

  • Callable可以在任務(wù)結(jié)束之后提供一個(gè)返回值,返回值通過 FutureTask 進(jìn)行封裝,Runnable則不能;
  • Callable接口中的call()方法可以拋出異常,而Runnable()的run()方法不能拋出異常;
  • 運(yùn)行Callable可以拿到一個(gè)Future對(duì)象,Future對(duì)象表示異步計(jì)算的結(jié)果,提供了檢查計(jì)算是否完成的方法。當(dāng)調(diào)用future的get()方法以獲取結(jié)果時(shí),當(dāng)前線程就會(huì)阻塞,直到call()方法結(jié)束返回結(jié)果。
  • public static class CallableTest implements Callable<String>{@Overridepublic String call() throws Exception {return "hello word";} }public static void main(String[] args) { ExecutorService threadPool = Executors.newSingleThreadExecutor();//1.創(chuàng)建線程池Future<String> future = threadPool.submit(new CallableTest());//2.提交任務(wù)System.out.println("waiting thread to finish");try {System.out.println(future.get());//3.獲取返回值} catch (InterruptedException | ExecutionException e) {e.printStackTrace(); }

    運(yùn)行結(jié)果:

    以上三種執(zhí)行方式,前兩種執(zhí)行完后都沒有返回值,最后一種帶返回值。

    當(dāng)實(shí)現(xiàn)多線程時(shí),一般推薦實(shí)現(xiàn)Runnable接口。

    一個(gè)類是否可以同時(shí)繼承Thread與實(shí)現(xiàn)Runnable?

    可以。

    public class Test extends Thread implements Runnable{public static void main(String[] args){Thread t=new Thread();t.start();} }

    其中,Test類從Thread類繼承的run()被認(rèn)為是對(duì)Runnable接口中run()的實(shí)現(xiàn)。

    1.4?三種實(shí)現(xiàn)方式的比較

    • 實(shí)現(xiàn)Runnable接口:
      • 可以避免Java單繼承特性而帶來的局限;
      • 增強(qiáng)程序的健壯性,代碼能夠被多個(gè)線程共享,代碼與數(shù)據(jù)是獨(dú)立的;
      • 適合多個(gè)相同程序代碼的線程區(qū)處理同一資源的情況。
    • 繼承Thread類和實(shí)現(xiàn)Runnable方法啟動(dòng)線程都是使用start方法,然后JVM虛擬機(jī)將此線程放到就緒隊(duì)列中,如果有處理機(jī)可用, 則執(zhí)行run方法。
    • 實(shí)現(xiàn)Callable接口要實(shí)現(xiàn)call方法,并且線程執(zhí)行完畢后會(huì)有返回值。其他的兩種都是重寫run方法,沒有返回值。
    與50位技術(shù)專家面對(duì)面20年技術(shù)見證,附贈(zèng)技術(shù)全景圖

    總結(jié)

    以上是生活随笔為你收集整理的实现Java多线程的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。