Java之線程編程的4種方法實(shí)現(xiàn)案例講解
1、繼承Thread
public class T4 { public static void main(String[] args) { System.out.println(Thread.currentThread()); Thread t1 = new A1(); t1.start(); } } class A1 extends Thread{ @Override public void run() { for(int i=0;i<10;i++) { System.out.println("Thread:"+Thread.currentThread().getName()); } } }
2、實(shí)現(xiàn)Runnable接口
public class T3 { public static void main(String[] args) { System.out.println("Thread:"+Thread.currentThread().getName()); Thread t1 = new Thread(new A2()); t1.start(); } } class A2 implements Runnable{ @Override public void run() { int res =0; for(int i=0;i<10;i++) { res+=i; System.out.println("Thread:"+Thread.currentThread().getName()); } } }
3、使用Callable和Future接口創(chuàng)建線程
import java.util.concurrent.Callable; import java.util.concurrent.FutureTask; public class T2 { public static void main(String[] args) throws Exception { System.out.println("Test3:" + Thread.currentThread().getName()); Callable c = new A4(); FutureTask ft = new FutureTask(c); Thread t1 = new Thread(ft); t1.start(); Object res = ft.get(); System.out.println("結(jié)果:" + res); } } class A4 implements Callable { @Override public Object call() throws Exception { int res = 0; for (int i = 0; i < 10; i++) { res += i; System.out.println("Thread:" + Thread.currentThread().getName()); } return res; } }
4、使用線程池創(chuàng)建線程
享元模式
享元模式Flyweight Pattern主要用于減少創(chuàng)建對象的數(shù)量,以減少內(nèi)存占用和提高性能。這種類型的設(shè)計(jì)模式屬于 結(jié)構(gòu)型模式,它提供了減少對象數(shù)量從而改善應(yīng)用所需的對象結(jié)構(gòu)的方式
優(yōu)點(diǎn):大大減少對象的創(chuàng)建,降低系統(tǒng)內(nèi)存的使用,以提高程序的執(zhí)行效率。
缺點(diǎn):提高了系統(tǒng)的復(fù)雜度,需要分離出外部狀態(tài)和內(nèi)部狀態(tài),而且外部狀態(tài)具有固有化的性質(zhì),不應(yīng)該隨著內(nèi)部 狀態(tài)的變化而變化,否則會造成系統(tǒng)的混亂。
import java.util.concurrent.Callable; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; public class T1 { public static void main(String[] args) throws Exception { Future[] arr = new Future[5]; ExecutorService es = Executors.newFixedThreadPool(3); for (int i = 0; i < 5; i++) { arr[i] = es.submit(new A4()); } for (Future f : arr) { Object res = f.get(); System.out.println("結(jié)果為:" + res); } es.shutdown(); } } class A4 implements Callable { @Override public Object call() throws Exception { int res = 0; for (int i = 0; i < 10; i++) { res += i; System.out.println("Thread" + Thread.currentThread().getName()); } return res; } }
到此這篇關(guān)于Java之線程編程的4種方法實(shí)現(xiàn)案例講解的文章就介紹到這了,更多相關(guān)Java之線程編程的4種方法實(shí)現(xiàn)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
elasticsearch開發(fā)中data-streams使用解析
這篇文章主要為大家介紹了elasticsearch開發(fā)中data-streams使用解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08Java的數(shù)據(jù)類型和參數(shù)傳遞(詳解)
下面小編就為大家?guī)硪黄狫ava的數(shù)據(jù)類型和參數(shù)傳遞(詳解)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-07-07POI導(dǎo)出之Excel實(shí)現(xiàn)單元格的背景色填充問題
這篇文章主要介紹了POI導(dǎo)出之Excel實(shí)現(xiàn)單元格的背景色填充問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03SpringBoot集成ShedLock實(shí)現(xiàn)分布式定時(shí)任務(wù)流程詳解
ShedLock是一個(gè)鎖,官方解釋是他永遠(yuǎn)只是一個(gè)鎖,并非是一個(gè)分布式任務(wù)調(diào)度器。一般shedLock被使用的場景是,你有個(gè)任務(wù),你只希望他在單個(gè)節(jié)點(diǎn)執(zhí)行,而不希望他并行執(zhí)行,而且這個(gè)任務(wù)是支持重復(fù)執(zhí)行的2023-02-02SpringBoot中集成screw(螺絲釘)實(shí)現(xiàn)數(shù)據(jù)庫表結(jié)構(gòu)文檔生成方法
這篇文章主要介紹了SpringBoot中集成screw(螺絲釘)實(shí)現(xiàn)數(shù)據(jù)庫表結(jié)構(gòu)文檔生成,下面以連接mysql數(shù)據(jù)庫并生成html格式的數(shù)據(jù)庫結(jié)構(gòu)文檔為例,插件的使用方式除可以使用代碼外,還可以使用Maven插件的方式,需要的朋友可以參考下2024-07-07解決@DateTimeFormat格式化時(shí)間出錯(cuò)問題
這篇文章主要介紹了解決@DateTimeFormat格式化時(shí)間出錯(cuò)問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12SpringBoot自定義注解及AOP的開發(fā)和使用詳解
在公司項(xiàng)目中,如果需要做一些公共的功能,如日志等,最好的方式是使用自定義注解,自定義注解可以實(shí)現(xiàn)我們對想要添加日志的方法上添加,這篇文章基于日志功能來講講自定義注解應(yīng)該如何開發(fā)和使用,需要的朋友可以參考下2023-08-08Java之Pattern.compile函數(shù)用法詳解
這篇文章主要介紹了Java之Pattern.compile函數(shù)用法詳解,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08