一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語(yǔ)言|JavaScript|易語(yǔ)言|vb.net|

服務(wù)器之家 - 編程語(yǔ)言 - Java教程 - java多線程Thread-per-Message模式詳解

java多線程Thread-per-Message模式詳解

2021-05-05 11:48小明是個(gè)程序猿 Java教程

本篇文章給大家分享java多線程Thread-per-Message模式的相關(guān)知識(shí)點(diǎn),對(duì)此有興趣的朋友參考學(xué)習(xí)下。

thread-per-message模式(這項(xiàng)工作就交給你了)

當(dāng)你很忙碌的時(shí)候,這個(gè)時(shí)候公司樓下有個(gè)快遞,于是你委托你的同事幫你拿一下你的快遞,這樣你就可以繼續(xù)做自己的工作了

在thread-per-message模式中,消息的委托端和執(zhí)行端是不同的線程,消息的委托端會(huì)告訴執(zhí)行端線程,這個(gè)工作就交給你了

java多線程Thread-per-Message模式詳解

host類:

針對(duì)請(qǐng)求創(chuàng)建線程的類,主要通過(guò)開(kāi)啟新的線程,調(diào)用helper的handle,并將要打印的文字傳遞。

java" id="highlighter_857044">
?
1
2
3
4
5
6
7
8
9
10
11
12
public class host {
private final helper helper = new helper();
public void request(final int count,final char c){
system.out.println("request開(kāi)始");
new thread(){
public void run(){
helper.handle(count, c);
}
}.start();
system.out.println("request結(jié)束");
}
}

helper類:

提供字符顯示的功能,slowly方法模擬打印耗時(shí)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class helper {
public void handle(int count ,char c){
system.out.println("handle方法開(kāi)始");
for(int i=0;i<count;i++){
slowly();
system.out.print(c);
}
system.out.println("");
system.out.println("handle方法結(jié)束");
}
private void slowly(){
try {
thread.sleep(100);
} catch (interruptedexception e) {
// todo auto-generated catch block
e.printstacktrace();
}
}
}

main類:

創(chuàng)建host的實(shí)例,并調(diào)用request的方法

?
1
2
3
4
5
6
7
8
public static void main(string[] args) {
system.out.println("main begin");
host host = new host();
host.request(10, 'a');
host.request(20, 'b');
host.request(30, 'c');
system.out.println("main end");
}

測(cè)試結(jié)果:

main begin

request方法開(kāi)始了

request方法結(jié)束

request方法開(kāi)始了

request方法結(jié)束

request方法開(kāi)始了

request方法結(jié)束

main end

handle方法開(kāi)始

handle方法開(kāi)始

handle方法開(kāi)始

bacbacacbacbacbacbacbacbacba

handle方法結(jié)束

cbcbcbcbcbcbcbcbcbcbcb

handle方法結(jié)束

cccccccccc

handle方法結(jié)束

從運(yùn)行的結(jié)果可以看出,request方法,并沒(méi)有等待handle方法執(zhí)行結(jié)束后再執(zhí)行,而是調(diào)用handle方法后就返回到request方法中,直到運(yùn)行結(jié)束,所以相當(dāng)于request方法將所要進(jìn)行的打印一定數(shù)量字符的工作轉(zhuǎn)交給了handle方法,而request方法則可以再執(zhí)行笨方法中的其他的語(yǔ)句,不必等待handle方法完成。這也同時(shí)告訴我們,當(dāng)某些工作比較耗時(shí)時(shí),則可以通過(guò)這種模式啟動(dòng)新的線程來(lái)執(zhí)行處理。可以將此模式應(yīng)用于服務(wù)器,這樣就可以減少服務(wù)器的響應(yīng)時(shí)間。

講解一下進(jìn)程和線程:

線程和進(jìn)程最大的區(qū)別就是內(nèi)存是否共存。

每個(gè)進(jìn)程有自己的獨(dú)立的內(nèi)存空間,一個(gè)進(jìn)程不可以擅自讀取和寫入其他的進(jìn)程的內(nèi)存,由于進(jìn)程的內(nèi)存空間是彼此獨(dú)立的,所以一個(gè)進(jìn)程無(wú)需擔(dān)心被其他的進(jìn)程所破壞。

線程之間是可以共存的,一個(gè)線程向?qū)嵗袑懭雰?nèi)容,其他線程就可以讀取該實(shí)例的內(nèi)容,由于多個(gè)線程可以訪問(wèn)同一個(gè)實(shí)例,我們就需要保證其正確執(zhí)行互斥處理。

host設(shè)計(jì)優(yōu)化:

1.使用java.util.concurrent包下的threadfactory接口設(shè)計(jì)host類

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class host {
public void request(final int count,final char c){
system.out.println("request方法開(kāi)始了");
threadfactory.newthread(
new runnable() {
@override
public void run() {
// todo auto-generated method stub
helper.handle(count, c);
}
 }
).start();;
system.out.println("request方法結(jié)束");
}
}

對(duì)應(yīng)的host實(shí)例化對(duì)象:

host host = new host(executors.defaultthreadfactory());

這樣設(shè)計(jì)的優(yōu)勢(shì)在于,原來(lái)的使用new創(chuàng)建的實(shí)例代碼依賴于java.lang.thread類,無(wú)法控制創(chuàng)建線程的部分,可復(fù)用性較低,假如使用threadfactory來(lái)保存對(duì)應(yīng)類的對(duì)象,調(diào)用newthread方法創(chuàng)建新的線程,這樣便實(shí)現(xiàn)了線程的創(chuàng)建,這樣不再依賴于thread類,而是取決于構(gòu)造函數(shù)中傳入的threadfactory對(duì)象,實(shí)現(xiàn)了控制線程創(chuàng)建的細(xì)節(jié)。

使用java.util.concurrent.executor接口重新設(shè)計(jì)host類:

前面的threadfactory接口隱藏了線程創(chuàng)建的細(xì)節(jié),但是并未隱藏線程創(chuàng)建的操作,如果使用executor接口,那么線程創(chuàng)建的操作也會(huì)被隱藏起來(lái)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class host{
private final helper helper = new helper();
private final executor executor;
public host(executor executor){
this.executor = executor;
}
public void request(final int count,final char c){
system.out.println("request方法開(kāi)始了");
executor.execute(new runnable() {
@override
public void run() {
// todo auto-generated method stub
helper.handle(count, c);
}
});
system.out.println("request方法結(jié)束");
}
}

使用java.util.concurrent.scheduledexecutorservice類創(chuàng)建,其可以實(shí)現(xiàn)調(diào)度運(yùn)行

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class host{
private final helper helper = new helper();
private final scheduledexecutorservice scheduledexecutorservice;
public host(scheduledexecutorservice scheduledexecutorservice){
this.scheduledexecutorservice = scheduledexecutorservice;
}
public void request(final int count,final char c){
system.out.println("request方法開(kāi)始了");
scheduledexecutorservice.schedule(new runnable() {
@override
public void run() {
// todo auto-generated method stub
helper.handle(count, c);
}
}, 3l, timeunit.seconds);
system.out.println("request方法結(jié)束");
}
}

測(cè)試主函數(shù)入口:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
scheduledexecutorservice scheduledexecutorservice = executors.newscheduledthreadpool(5);
host host = new host(
scheduledexecutorservice
);
try {
host.request(10, 'a');
host.request(20, 'b');
host.request(30, 'c');
} catch (exception e) {
// todo auto-generated catch block
e.printstacktrace();
}finally{
scheduledexecutorservice.shutdown();
system.out.println("main end");
}

總結(jié)

client 角色調(diào)用host角色的request方法發(fā)來(lái)的請(qǐng)求,該請(qǐng)求的實(shí)際處理則交給helper的handle去執(zhí)行,然而,如果client直接從request中調(diào)用handle方法,那么直到實(shí)際操作結(jié)束之前,都無(wú)法從handle方法返回(request返回),這樣一來(lái)request的響應(yīng)性能就下降了,因此,host角色會(huì)啟動(dòng)用于處理來(lái)自client角色請(qǐng)求的新線程,并讓該線程來(lái)調(diào)用handle,這樣一來(lái)發(fā)出請(qǐng)求的線程便可以立即從handle中返回。這就是thread-per-message模式。

原文鏈接:https://blog.csdn.net/qq_31350373/article/details/80454306

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 四虎在线观看 | 亚洲精品中文字幕第一区 | 国产精品秒播无毒不卡 | 亚洲 综合 自拍 精品 在线 | 久久re这里精品23 | 女人把私密部位张开让男人桶 | 成人高辣h视频一区二区在线观看 | 久久九九亚洲精品 | 香蕉精品国产高清自在自线 | 成人中文字幕在线观看 | 欧美调教打屁股spank视频 | 精品9e精品视频在线观看 | 暖暖影院日本版 | 亚洲精品卡一卡2卡3卡4卡 | 国产成人精品在线 | 亚洲天堂免费观看 | 无人在线高清免费看 | 亚洲国产精品久久卡一 | 从后面撕开老师的丝袜动态图 | bnb99八度免费影院 | 男生和女生搞逼逼 | 天天舔天天干天天操 | 日本视频在线播放 | 九九国产视频 | 2021年国内自拍 | 欧美一级在线 | 被巨大黑人的翻白眼 | 国产成人激情视频 | 亚洲福利精品电影在线观看 | 4455永久在线视频观看 | 秘书在办公室疯狂被hd | 亚洲区在线 | 亚洲狠狠综合久久 | 日本一区二区在线不卡 | 午夜视频一区 | 99热精品成人免费观看 | 午夜亚洲视频 | 深夜福利一区 | 无遮无挡免费视频 | tube4欧美4 | 双性双根 |