溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Java中怎么實現(xiàn)線程編程

發(fā)布時間:2021-08-02 11:35:54 來源:億速云 閱讀:103 作者:Leah 欄目:開發(fā)技術(shù)

Java中怎么實現(xiàn)線程編程,針對這個問題,這篇文章詳細介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。

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)存占用和提高性能。這種類型的設(shè)計模式屬于 結(jié)構(gòu)型模式,它提供了減少對象數(shù)量從而改善應(yīng)用所需的對象結(jié)構(gòu)的方式
優(yōu)點:大大減少對象的創(chuàng)建,降低系統(tǒng)內(nèi)存的使用,以提高程序的執(zhí)行效率。
缺點:提高了系統(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中怎么實現(xiàn)線程編程問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注億速云行業(yè)資訊頻道了解更多相關(guān)知識。

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI