Java之線程編程的4種方法實現(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、實現(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)存占用和提高性能。這種類型的設計模式屬于 結(jié)構(gòu)型模式,它提供了減少對象數(shù)量從而改善應用所需的對象結(jié)構(gòu)的方式
優(yōu)點:大大減少對象的創(chuàng)建,降低系統(tǒng)內(nèi)存的使用,以提高程序的執(zhí)行效率。
缺點:提高了系統(tǒng)的復雜度,需要分離出外部狀態(tài)和內(nèi)部狀態(tài),而且外部狀態(tài)具有固有化的性質(zhì),不應該隨著內(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;
}
}
到此這篇關于Java之線程編程的4種方法實現(xiàn)案例講解的文章就介紹到這了,更多相關Java之線程編程的4種方法實現(xiàn)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
SpringBoot如何在線程中獲取@Service Bean類
這篇文章主要介紹了SpringBoot如何在線程中獲取@Service Bean類,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02
Spring Boot整合Mybatis Plus和Swagger2的教程詳解
這篇文章主要介紹了Spring Boot整合Mybatis Plus和Swagger2的教程,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-02-02
Java語言之LinkedList和鏈表的實現(xiàn)方法
LinkedList是由傳統(tǒng)的鏈表數(shù)據(jù)結(jié)構(gòu)演變而來的,鏈表是一種基本的數(shù)據(jù)結(jié)構(gòu),它可以動態(tài)地增加或刪除元素,下面這篇文章主要給大家介紹了關于Java語言之LinkedList和鏈表的實現(xiàn)方法,需要的朋友可以參考下2023-05-05
springboot之security?FilterSecurityInterceptor的使用要點記錄
這篇文章主要介紹了springboot之security?FilterSecurityInterceptor的使用要點記錄,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-12-12

