|NO.Z.00093|——————————|BigDataEnd|——|Java&多執行緒.V05|------------------------------------------------|Java.v05|匿名內部類|實現執行緒|建立啟動|
阿新 • • 發佈:2022-04-04
[BigDataJava:Java&多執行緒.V05] [BigDataJava.核心類庫] [|章節三|多執行緒|匿名內部類的方式實現執行緒建立和啟動|]
一、匿名內部類的方式實現執行緒建立和啟動
### --- 匿名內部類的方式
——> 使用匿名內部類的方式來建立和啟動執行緒。
二、程式設計程式碼
三、編譯列印package com.yanqi.task18; public class ThreadNoNameTest { public static void main(String[] args) { // 匿名內部類的語法格式:父類/介面型別 引用變數名 = new 父類/介面型別() { 方法的重寫 }; // 1.使用繼承加匿名內部類的方式建立並啟動執行緒 /*Thread t1 = new Thread() { @Override public void run() { System.out.println("張三說:在嗎?"); } }; t1.start();*/ new Thread() { @Override public void run() { System.out.println("張三說:在嗎?"); } }.start(); // 2.使用實現介面加匿名內部類的方式建立並啟動執行緒 /*Runnable ra = new Runnable() { @Override public void run() { System.out.println("李四說:不在。"); } }; Thread t2 = new Thread(ra); t2.start();*/ /*new Thread(new Runnable() { @Override public void run() { System.out.println("李四說:不在。"); } }).start();*/ // Java8開始支援lambda表示式: (形參列表)->{方法體;} /*Runnable ra = ()-> System.out.println("李四說:不在。"); new Thread(ra).start();*/ new Thread(()-> System.out.println("李四說:不在。")).start(); } }
D:\JAVA\jdk-11.0.2\bin\java.exe "-javaagent:D:\IntelliJIDEA\IntelliJ IDEA 2019.3.3\lib\idea_rt.jar=50351:D:\IntelliJIDEA\IntelliJ IDEA 2019.3.3\bin" -Dfile.encoding=UTF-8 -classpath E:\NO.Z.10000——javaproject\NO.H.00001.javase\javase\out\production\javase com.yanqi.task18.ThreadNoNameTest 張三說:在嗎? 李四說:不在。 Process finished with exit code 0
===============================END===============================
Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart ——W.S.Landor
來自為知筆記(Wiz)