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)建對(duì)象的數(shù)量,以減少內(nèi)存占用和提高性能。這種類型的設(shè)計(jì)模式屬于 結(jié)構(gòu)型模式,它提供了減少對(duì)象數(shù)量從而改善應(yīng)用所需的對(duì)象結(jié)構(gòu)的方式
優(yōu)點(diǎn):大大減少對(duì)象的創(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)的變化而變化,否則會(huì)造成系統(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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
MybatisPlus修改時(shí)空字段無(wú)法修改的解決方案
這篇文章主要介紹了MybatisPlus修改時(shí)空字段無(wú)法修改的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09
java面試題解LeetCode27二叉樹的鏡像實(shí)例
這篇文章主要為大家介紹了java面試題解LeetCode27二叉樹的鏡像實(shí)例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01
SpringBoot如何在線程中獲取@Service Bean類
這篇文章主要介紹了SpringBoot如何在線程中獲取@Service Bean類,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02
JDBC以反射機(jī)制加載類注冊(cè)驅(qū)動(dòng)連接MySQL
這篇文章介紹了JDBC以反射機(jī)制加載類注冊(cè)驅(qū)動(dòng)連接MySQL的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-01-01
Spring Boot整合Mybatis Plus和Swagger2的教程詳解
這篇文章主要介紹了Spring Boot整合Mybatis Plus和Swagger2的教程,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-02-02
Java語(yǔ)言之LinkedList和鏈表的實(shí)現(xiàn)方法
LinkedList是由傳統(tǒng)的鏈表數(shù)據(jù)結(jié)構(gòu)演變而來(lái)的,鏈表是一種基本的數(shù)據(jù)結(jié)構(gòu),它可以動(dòng)態(tài)地增加或刪除元素,下面這篇文章主要給大家介紹了關(guān)于Java語(yǔ)言之LinkedList和鏈表的實(shí)現(xiàn)方法,需要的朋友可以參考下2023-05-05
springboot之security?FilterSecurityInterceptor的使用要點(diǎn)記錄
這篇文章主要介紹了springboot之security?FilterSecurityInterceptor的使用要點(diǎn)記錄,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12

