thread-per-message模式(這項(xiàng)工作就交給你了)
當(dāng)你很忙碌的時(shí)候,這個(gè)時(shí)候公司樓下有個(gè)快遞,于是你委托你的同事幫你拿一下你的快遞,這樣你就可以繼續(xù)做自己的工作了
在thread-per-message模式中,消息的委托端和執(zhí)行端是不同的線程,消息的委托端會(huì)告訴執(zhí)行端線程,這個(gè)工作就交給你了
host類:
針對(duì)請(qǐng)求創(chuàng)建線程的類,主要通過(guò)開(kāi)啟新的線程,調(diào)用helper的handle,并將要打印的文字傳遞。
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