一、繼承Thread,重寫run方法
通過自定義一個類(這里起名為:MyThread),繼承Thread類,重寫run方法,最后在main方法中new出MyThread實例,調(diào)用這個實例的繼承的Thread類的start方法創(chuàng)建一個線程。
class MyThread extends Thread {
@Override
public void run() {
System.out.println("繼承Thread,重寫run方法創(chuàng)建線程");
}
}
public class Main {
public static void main(String[] args) {
MyThread myThread = new MyThread();
myThread.start();
}
}
二、實現(xiàn)Runnable接口,重寫run方法
通過自定義一個類(這里起名為:MyRunnable)實現(xiàn)Runnable接口,重寫run方法,最后在main方法new出MyRunnable實例和Thread實例,最后通過start方法創(chuàng)建并啟動線程。
class MyRunnable implements Runnable {
@Override
public void run() {
System.out.println("實現(xiàn)Runnable接口,重寫run方法");
}
}
public class Main {
public static void main(String[] args) {
MyRunnable myRunnable = new MyRunnable();
Thread thread = new Thread(myRunnable);
thread.start();
}
}
三、使用匿名內(nèi)部類創(chuàng)建 Thread 子類對象
直接創(chuàng)建Thread子類,同時實例化出一個對象,重寫run方法,最后通過start方法創(chuàng)建并啟動線程。
public class Main {
public static void main(String[] args) {
Thread thread = new Thread() {
@Override
public void run() {
System.out.println("使用匿名內(nèi)部類創(chuàng)建 Thread 子類對象");
}
};
thread.start();
}
}
四、使用匿名內(nèi)部類,實現(xiàn)Runnable接口
通過使用使用匿名內(nèi)部類,實現(xiàn)Runnable接口作為Thread構(gòu)造方法的參數(shù),最后通過start創(chuàng)建并啟動線程。
public class Main {
public static void main(String[] args) {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
System.out.println("使用匿名內(nèi)部類,實例Runnable接口作為構(gòu)造參數(shù)");
}
});
thread.start();
}
}
五、lambda表達式
lambda本質(zhì)上就是一個“匿名函數(shù)”,()表示函數(shù)的形參,{}表示函數(shù)體,->特殊語法,表示它是lambda表達式(->是從C++那里抄來的)。
public class Main {
public static void main(String[] args) {
Thread thread = new Thread(() -> {
System.out.println("使用lambda表示創(chuàng)建線程");
});
thread.start();
}
}
六、實現(xiàn)Callable接口
通過自定義類(這里起名為:MyCallable),實現(xiàn)Callable接口,重寫call方法(call方法可以理解為線程需要執(zhí)行的任務),并且?guī)в蟹祷刂?,這個返回表示一個計算結(jié)果,如果無法計算結(jié)果,則引發(fā)Exception異常,接著創(chuàng)建Callable實例,使用FutrueTast類包裝Callable對象,F(xiàn)utureTask是一個包裝器,需要接收Callable實例來創(chuàng)建,并且有兩個構(gòu)造函數(shù),一個參數(shù)只有Callable對象,另一個參數(shù)不僅有Callable對象,還有一個泛型的result參數(shù),最后使用FutureTask對象作為Thread的構(gòu)造參數(shù),通過start方法創(chuàng)建并啟動線程;
class MyCallableTest implements Callable<Integer> {
@Override
public Integer call() throws Exception {
System.out.println("創(chuàng)建線程:" + Thread.currentThread().getName());
return 2;
}
}
public class Main {
public static void main(String[] args) throws ExecutionException, InterruptedException {
FutureTask<Integer> task = new FutureTask<>(new MyCallableTest());
Thread thread = new Thread(task);
thread.start();
System.out.println("創(chuàng)建線程的返回結(jié)果為:" + task.get());
}
}
七、使用線程池創(chuàng)建線程
?在Java中,線程池的本體叫ThreadPoolExecutor,他的構(gòu)造方法寫起來十分麻煩,為了簡化構(gòu)造方法,標準庫就提供了一系列工廠方法,簡化使用;
什么是工廠模式?
????????工廠模式,將創(chuàng)建產(chǎn)品實例的權(quán)利移交工廠,我們不再通過new來創(chuàng)建我們所需的對象,而是通過工廠來獲取我們需要的產(chǎn)品。降低了產(chǎn)品使用者與使用者之間的耦合關系;文章來源:http://www.zghlxwxcb.cn/news/detail-708067.html
也就是說這里創(chuàng)建線程池沒有顯式new,而是通過Executors這個靜態(tài)方法newCaChedThreadPool來完成的;文章來源地址http://www.zghlxwxcb.cn/news/detail-708067.html
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Pool {
public static void main(String[] args) {
ExecutorService pool = Executors.newCachedThreadPool();
pool.submit(new Runnable() {
@Override
public void run() {
//執(zhí)行業(yè)務邏輯
for(int i = 1; i <= 100; i++) {
System.out.println("線程:" + Thread.currentThread().getName() + "執(zhí)行了任務" + i + "~");
}
}
});
pool.submit(new Runnable() {
@Override
public void run() {
//執(zhí)行業(yè)務邏輯
for(int i = 101; i <= 200; i++) {
System.out.println("線程:" + Thread.currentThread().getName() + "執(zhí)行了任務" + i + "~");
}
}
});
pool.submit(new Runnable() {
@Override
public void run() {
//執(zhí)行業(yè)務邏輯
for(int i = 201; i <= 300; i++) {
System.out.println("線程:" + Thread.currentThread().getName() + "執(zhí)行了任務" + i + "~");
}
}
});
}
}
到了這里,關于java創(chuàng)建多線程的七種方式的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!