多线程三种创建方式
方法一:繼承Thread
Thread
package com.bjsxt.create;/*** @author dell* @data 2021/3/2* 創(chuàng)建一個(gè)線程對(duì)象,并啟動(dòng)線程** 注意:啟動(dòng)main方法,自動(dòng)創(chuàng)建main線程* * thread.join() 阻塞烏龜線程,烏龜執(zhí)行完,兔子才有機(jī)會(huì)* * 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 {//啟動(dòng)烏龜線程Thread thread = new TortoiseThread();thread.setName("烏龜1線程");thread.start(); //啟動(dòng)一個(gè)新的線程thread.join(); //阻塞烏龜1線程,其他執(zhí)行完,烏龜1才有機(jī)會(huì)Thread thread2 = new TortoiseThread();thread2.setName("烏龜2線程");thread2.start(); //啟動(dòng)一個(gè)新的線程//兔子也在跑Thread.currentThread().setName("主線程");while (true){System.out.println("兔子領(lǐng)先了" + Thread.currentThread().getName() + " " +Thread.currentThread().getPriority());}} }main
package com.bjsxt.create;/*** @author dell* @data 2021/3/2* 定義一個(gè)烏龜線程類*/ public class TortoiseThread extends Thread{/*** 線程體:線程要執(zhí)行的任務(wù)*/@Overridepublic void run() {while(true){System.out.println("烏龜領(lǐng)先" +this.getName()+ " "+this.getPriority());}}}方法二:實(shí)現(xiàn)Runnable
Runnable
package com.bjsxt.create2;/*** @author dell* @data 2021/3/2* 定義線程方式2: 實(shí)現(xiàn)Runnable接口* 創(chuàng)建線程對(duì)象:先定義一個(gè)任務(wù),Runnable runnable = new TortoiseRunnable();* 再創(chuàng)建一個(gè)線程,Thread thread1 = new Thread(runnable);** ?兩種方式的優(yōu)缺點(diǎn)* 方式1:繼承Thread類* 缺點(diǎn):Java單繼承,無(wú)法繼承其他類* 優(yōu)點(diǎn):代碼稍微簡(jiǎn)單* 方式2:實(shí)現(xiàn)Runnable接口* 優(yōu)點(diǎn) 還可以去繼承其他類 便于多個(gè)線程共享同一個(gè)資源* 缺點(diǎn):代碼略有繁瑣* 實(shí)際開(kāi)發(fā)中,方式2使用更多一些**/ public class TortoiseRunnable implements Runnable {@Overridepublic void run() {while (true){System.out.println("烏龜領(lǐng)先了" + 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) {//兩個(gè)烏龜在跑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();//一個(gè)兔子在跑Thread.currentThread().setName("主線程");while (true){System.out.println("兔子領(lǐng)先了" + Thread.currentThread().getName() + " " +Thread.currentThread().getPriority());}} }優(yōu)化:使用匿名內(nèi)部類來(lái)創(chuàng)建線程對(duì)象
package com.bjsxt.create2;/*** @author dell* @data 2021/3/2* ?可以使用匿名內(nèi)部類來(lái)創(chuàng)建線程對(duì)象*/ public class TextThread2 {public static void main(String[] args) {//兩個(gè)烏龜在跑Runnable runnable = new Runnable(){@Overridepublic void run() {while (true){System.out.println("烏龜領(lǐng)先了" + 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();//一個(gè)兔子在跑Thread.currentThread().setName("主線程");while (true){System.out.println("兔子領(lǐng)先了" + Thread.currentThread().getName() + " " +Thread.currentThread().getPriority());}} }方法三:繼承Callable接口
優(yōu)點(diǎn):有返回值,可以拋出異常
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 {//創(chuàng)建一個(gè)線程對(duì)象Callable<Integer> callable = new RandomCallable();FutureTask<Integer> task = new FutureTask(callable);Thread thread = new Thread(task);//啟動(dòng)線程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);} }總結(jié)
- 上一篇: JDBC登录功能实现
- 下一篇: war thunder配置要求?