新聞中心
創(chuàng)建多線程的方法
1、繼承Thread類

創(chuàng)建一個(gè)新的類,繼承自Thread類,然后重寫run()方法,在run()方法中編寫需要在新線程中執(zhí)行的任務(wù),最后創(chuàng)建該類的對(duì)象,并調(diào)用start()方法啟動(dòng)線程。
class MyThread extends Thread {
@Override
public void run() {
// 在這里編寫需要在新線程中執(zhí)行的任務(wù)
}
}
public class Main {
public static void main(String[] args) {
MyThread myThread = new MyThread();
myThread.start();
}
}
2、實(shí)現(xiàn)Runnable接口
創(chuàng)建一個(gè)新的類,實(shí)現(xiàn)Runnable接口,然后重寫run()方法,在run()方法中編寫需要在新線程中執(zhí)行的任務(wù),最后創(chuàng)建該類的對(duì)象,將其作為參數(shù)傳遞給Thread類的構(gòu)造函數(shù),并調(diào)用start()方法啟動(dòng)線程。
class MyRunnable implements Runnable {
@Override
public void run() {
// 在這里編寫需要在新線程中執(zhí)行的任務(wù)
}
}
public class Main {
public static void main(String[] args) {
MyRunnable myRunnable = new MyRunnable();
Thread thread = new Thread(myRunnable);
thread.start();
}
}
3、實(shí)現(xiàn)Callable接口和FutureTask類
創(chuàng)建一個(gè)新的類,實(shí)現(xiàn)Callable接口,然后重寫call()方法,在call()方法中編寫需要在新線程中執(zhí)行的任務(wù),接著創(chuàng)建一個(gè)FutureTask對(duì)象,將實(shí)現(xiàn)了Callable接口的類的對(duì)象作為參數(shù)傳遞給FutureTask的構(gòu)造函數(shù),最后調(diào)用FutureTask對(duì)象的get()方法獲取任務(wù)的返回值。
import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; import java.util.concurrent.FutureTask; class MyCallable implements Callable{ @Override public Integer call() throws Exception { // 在這里編寫需要在新線程中執(zhí)行的任務(wù),并返回結(jié)果 return null; } } public class Main { public static void main(String[] args) throws InterruptedException, ExecutionException { MyCallable myCallable = new MyCallable(); FutureTask futureTask = new FutureTask<>(myCallable); Thread thread = new Thread(futureTask); thread.start(); Integer result = futureTask.get(); // 等待任務(wù)完成并獲取結(jié)果 } }
4、利用線程池(ExecutorService)和Runnable接口實(shí)現(xiàn)懶漢式單例模式(推薦)
使用線程池來管理線程,可以避免手動(dòng)創(chuàng)建和管理線程帶來的繁瑣,通過實(shí)現(xiàn)Runnable接口并傳入一個(gè)實(shí)現(xiàn)了單例模式的對(duì)象,可以實(shí)現(xiàn)懶漢式單例模式,線程池會(huì)自動(dòng)分配線程來執(zhí)行任務(wù)。
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
class MyRunnable implements Runnable {
private static final MyRunnable instance = new MyRunnable(); // 實(shí)現(xiàn)懶漢式單例模式的唯一實(shí)例對(duì)象
private final Object lock = new Object(); // 防止多線程同時(shí)訪問實(shí)例對(duì)象時(shí)發(fā)生沖突的鎖對(duì)象
private MyRunnable() {} // 將構(gòu)造方法設(shè)為私有,防止外部直接創(chuàng)建實(shí)例對(duì)象
public static MyRunnable getInstance() { // 實(shí)現(xiàn)靜態(tài)工廠方法,提供獲取唯一實(shí)例對(duì)象的途徑
synchronized (lock) { // 當(dāng)多個(gè)線程同時(shí)訪問時(shí),使用synchronized關(guān)鍵字保證只有一個(gè)線程能夠進(jìn)入同步代碼塊,從而避免多線程同時(shí)修改實(shí)例對(duì)象的問題
if (instance == null) { // 如果實(shí)例對(duì)象尚未創(chuàng)建,則創(chuàng)建一個(gè)新的實(shí)例對(duì)象并返回給調(diào)用者
instance = new MyRunnable();
}
return instance; // 如果實(shí)例對(duì)象已經(jīng)創(chuàng)建,則直接返回該實(shí)例對(duì)象給調(diào)用者,無需再次創(chuàng)建新的實(shí)例對(duì)象浪費(fèi)資源
}
}
@Override
public void run() { // 在run()方法中編寫需要在新線程中執(zhí)行的任務(wù)
}
}
當(dāng)前題目:c創(chuàng)建多線程的方法有哪些
本文地址:http://m.fisionsoft.com.cn/article/djdijjg.html


咨詢
建站咨詢
