多线程三种创建方式
方法一:繼承Thread
Thread
package com.bjsxt.create;/*** @author dell* @data 2021/3/2* 創建一個線程對象,并啟動線程** 注意:啟動main方法,自動創建main線程* * thread.join() 阻塞烏龜線程,烏龜執行完,兔子才有機會* * Thread類的常用方法:* public void run()* thread.start();* this.getName()* this.getPriority()* thread.setNam()* Thread.currentThread().getPriority()**/ public class TextThread {public static void main(String[] args) throws InterruptedException {//啟動烏龜線程Thread thread = new TortoiseThread();thread.setName("烏龜1線程");thread.start(); //啟動一個新的線程thread.join(); //阻塞烏龜1線程,其他執行完,烏龜1才有機會Thread thread2 = new TortoiseThread();thread2.setName("烏龜2線程");thread2.start(); //啟動一個新的線程//兔子也在跑Thread.currentThread().setName("主線程");while (true){System.out.println("兔子領先了" + Thread.currentThread().getName() + " " +Thread.currentThread().getPriority());}} }main
package com.bjsxt.create;/*** @author dell* @data 2021/3/2* 定義一個烏龜線程類*/ public class TortoiseThread extends Thread{/*** 線程體:線程要執行的任務*/@Overridepublic void run() {while(true){System.out.println("烏龜領先" +this.getName()+ " "+this.getPriority());}}}方法二:實現Runnable
Runnable
package com.bjsxt.create2;/*** @author dell* @data 2021/3/2* 定義線程方式2: 實現Runnable接口* 創建線程對象:先定義一個任務,Runnable runnable = new TortoiseRunnable();* 再創建一個線程,Thread thread1 = new Thread(runnable);** ?兩種方式的優缺點* 方式1:繼承Thread類* 缺點:Java單繼承,無法繼承其他類* 優點:代碼稍微簡單* 方式2:實現Runnable接口* 優點 還可以去繼承其他類 便于多個線程共享同一個資源* 缺點:代碼略有繁瑣* 實際開發中,方式2使用更多一些**/ public class TortoiseRunnable implements Runnable {@Overridepublic void run() {while (true){System.out.println("烏龜領先了" + Thread.currentThread().getName() + " " +Thread.currentThread().getPriority());}} }main
package com.bjsxt.create2;/*** @author dell* @data 2021/3/2*/ public class TextThread {public static void main(String[] args) {//兩個烏龜在跑Runnable runnable = new TortoiseRunnable();Thread thread1 = new Thread(runnable);thread1.setName("烏龜1線程");thread1.setPriority(Thread.MAX_PRIORITY);thread1.start();//Runnable runnable1 = new TortoiseRunnable();Thread thread2 = new Thread(runnable,"烏龜2線程");thread2.start();//一個兔子在跑Thread.currentThread().setName("主線程");while (true){System.out.println("兔子領先了" + Thread.currentThread().getName() + " " +Thread.currentThread().getPriority());}} }優化:使用匿名內部類來創建線程對象
package com.bjsxt.create2;/*** @author dell* @data 2021/3/2* ?可以使用匿名內部類來創建線程對象*/ public class TextThread2 {public static void main(String[] args) {//兩個烏龜在跑Runnable runnable = new Runnable(){@Overridepublic void run() {while (true){System.out.println("烏龜領先了" + Thread.currentThread().getName() + " " +Thread.currentThread().getPriority());}}};Thread thread1 = new Thread(runnable);thread1.setName("烏龜1線程");thread1.setPriority(Thread.MAX_PRIORITY);thread1.start();Runnable runnable1 = new TortoiseRunnable();Thread thread2 = new Thread(runnable,"烏龜2線程");thread2.start();//一個兔子在跑Thread.currentThread().setName("主線程");while (true){System.out.println("兔子領先了" + Thread.currentThread().getName() + " " +Thread.currentThread().getPriority());}} }方法三:繼承Callable接口
優點:有返回值,可以拋出異常
package com.bjsxt.create3;import java.util.Random; import java.util.concurrent.*;/*** @author dell CTRL+z 返回上一步* @data 2021/3/2 有返回值,可以拋出異常*/ public class RandomCallable implements Callable <Integer> {@Overridepublic Integer call() throws Exception {Thread.sleep(6000);return new Random().nextInt(10);}public static void main(String[] args) throws ExecutionException, InterruptedException {//創建一個線程對象Callable<Integer> callable = new RandomCallable();FutureTask<Integer> task = new FutureTask(callable);Thread thread = new Thread(task);//啟動線程thread.start();//獲取返回值System.out.println(task.isDone());int result= task.get(); //得不到返回值就一直等待/*int result= 0;try {result = task.get(3, TimeUnit.SECONDS);} catch (TimeoutException e) {e.printStackTrace();}*/System.out.println(task.isDone());System.out.println(result);} }總結
- 上一篇: JDBC登录功能实现
- 下一篇: 多线程三种同步方式(模拟银行取款)