java創(chuàng)建線程的方式有幾種?這篇文章運(yùn)用了實(shí)例代碼展示,代碼非常詳細(xì),可供感興趣的小伙伴們參考借鑒,希望對大家有所幫助。
創(chuàng)新互聯(lián)專注于企業(yè)成都全網(wǎng)營銷、網(wǎng)站重做改版、福鼎網(wǎng)站定制設(shè)計、自適應(yīng)品牌網(wǎng)站建設(shè)、H5場景定制、商城網(wǎng)站建設(shè)、集團(tuán)公司官網(wǎng)建設(shè)、成都外貿(mào)網(wǎng)站建設(shè)公司、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁設(shè)計等建站業(yè)務(wù),價格優(yōu)惠性價比高,為福鼎等各大城市提供網(wǎng)站開發(fā)制作服務(wù)。
1、繼承Thread類
public class ThreadCreator extends Thread{ public static void main(String[] args) { //第一種方式: ThreadCreator creator = new ThreadCreator(); Thread thread = new Thread(creator,"線程1"); thread.start(); //第二種方式: Thread thread = new ThreadCreator(); thread.start(); //第三種方式: new ThreadCreator().start(); } @Override public void run() { System.out.println(Thread.currentThread().getName() + "run"); } }
2、實(shí)現(xiàn)Runnable接口
(免費(fèi)學(xué)習(xí)視頻教程分享:java視頻教程)
public class ThreadCreator implements Runnable{ public static void main(String[] args) { ThreadCreator creator = new ThreadCreator(); Thread thread = new Thread(creator,"線程1"); thread.start(); } @Override public void run() { System.out.println(Thread.currentThread().getName() + "run"); } }
3、實(shí)現(xiàn)Callable接口
public class ThreadCreator implements Callable<Integer> { public static void main(String[] args) throws ExecutionException, InterruptedException { ThreadCreator creator = new ThreadCreator(); FutureTask futureTask = new FutureTask(creator); Thread thread = new Thread(futureTask,"線程"); thread.start(); System.out.println(futureTask.get()); } @Override public Integer call() { return 1024; } }
4、線程池ExecutorService
public class ThreadCreator{ static ExecutorService service = Executors.newFixedThreadPool(5); public static void main(String[] args) throws ExecutionException, InterruptedException { //execute無返回值 service.execute(new ThreadTask(1,"1")); //submit有返回值 Future<Integer> result = service.submit(new ThreadTaskCall()); System.out.println(result.get()); service.shutdownNow(); } static class ThreadTask implements Runnable{ private int param1; private String param2; public ThreadTask(int param3,String param4){ this.param1 = param3; this.param2 = param4; } @Override public void run() { System.out.println(param1+param2); } } static class ThreadTaskCall implements Callable<Integer>{ @Override public Integer call() throws Exception { return 1024; } } }
線程池中submit和execute的區(qū)別:
(1)可接受的任務(wù)類型不一樣:execute只能接受Runnable任務(wù),submit還可以接受Callable任務(wù)。
(2)返回值:execute無返回值,任務(wù)一旦提交,無法在當(dāng)前線程中監(jiān)控執(zhí)行結(jié)果。submit有一個Future類型的返回值,用來接收返回值或響應(yīng)異常。通過get()方法獲取。
submit底層還是調(diào)用的execute,只是在此基礎(chǔ)上用future封裝了一層,并將執(zhí)行過程中產(chǎn)生的異常全部封裝在一個變量中:
public void run() { if (state != NEW || !UNSAFE.compareAndSwapObject(this, runnerOffset, null, Thread.currentThread())) return; try { Callable<V> c = callable; if (c != null && state == NEW) { V result; boolean ran; try { result = c.call(); ran = true; } catch (Throwable ex) { result = null; ran = false; setException(ex); } if (ran) set(result); } } finally { runner = null; int s = state; if (s >= INTERRUPTING) handlePossibleCancellationInterrupt(s); } } protected void setException(Throwable t) { if (UNSAFE.compareAndSwapInt(this, stateOffset, NEW, COMPLETING)) { outcome = t; UNSAFE.putOrderedInt(this, stateOffset, EXCEPTIONAL); // final state finishCompletion(); } }
另外,spring中的schedule注解借鑒使用了submit的處理方式。
5、匿名內(nèi)部類
public class ThreadCreator { public static void main(String[] args) { //繼承Thread類 new Thread() { @Override public void run() { System.out.println("extends Thread Class!"); } }.start(); //實(shí)現(xiàn)Runnable接口 new Thread(new Runnable() { @Override public void run() { System.out.println("implement Runnable!"); } }).start(); //實(shí)現(xiàn)Callable接口 new Thread(new FutureTask<Integer>(new Callable() { @Override public Integer call() throws Exception { return 1024; } })).start(); //lambda表達(dá)式 new Thread(() -> System.out.println("execute single code")).start(); new Thread(() -> { System.out.println("execute multiple code"); }).start(); } }
lambda線程池:
public class ThreadCreator { static ExecutorService service = Executors.newFixedThreadPool(5); static List list = new ArrayList(); public static void main(String[] args) { service.execute(() -> execute()); //無返回值 Future future = service.submit(() -> execute()); //有返回值 list.add(future); } public static void execute() { //do something } }
以上就是java創(chuàng)建線程的方式匯總,內(nèi)容較為全面,小編相信有部分知識點(diǎn)可能是我們?nèi)粘9ぷ骺赡軙姷交蛴玫降?。希望你能通過這篇文章學(xué)到更多知識。
文章標(biāo)題:java創(chuàng)建線程的方式有幾種?
文章源于:http://muchs.cn/article16/pdjgdg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供云服務(wù)器、動態(tài)網(wǎng)站、網(wǎng)站營銷、靜態(tài)網(wǎng)站、網(wǎng)站建設(shè)、軟件開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)