在JAVA平臺,實現異步調用的角色有如下三個角色:調用者 提貨單 真實數據
一個調用者在調用耗時操作,不能立即返回數據時,先返回一個提貨單.然后在過一斷時間后憑提貨單來獲取真正的數據.
去蛋糕店買蛋糕,不需要等蛋糕做出來(假設現做要很長時間),只需要領個提貨單就可以了(去干別的事情),等到蛋糕做好了,再拿提貨單取蛋糕就可以了。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
public class Main { public static void main(String[] args) { System.out.println( "main BEGIN" ); Host host = new Host(); Data data1 = host.request( 10 , 'A' ); Data data2 = host.request( 20 , 'B' ); Data data3 = host.request( 30 , 'C' ); System.out.println( "main otherJob BEGIN" ); try { Thread.sleep( 200 ); } catch (InterruptedException e) { } System.out.println( "main otherJob END" ); System.out.println( "data1 = " + data1.getContent()); System.out.println( "data2 = " + data2.getContent()); System.out.println( "data3 = " + data3.getContent()); System.out.println( "main END" ); } } |
這里的main類就相當于“顧客”,host就相當于“蛋糕店”,顧客向“蛋糕店”定蛋糕就相當于“發請求request”,返回的數據data是FutureData的實例,就相當于提貨單,而不是真正的“蛋糕”。在過一段時間后(sleep一段時間后),調用data1.getContent(),也就是拿提貨單獲取執行結果。
下面來看一下,顧客定蛋糕后,蛋糕店做了什么:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
public class Host { public Data request( final int count, final char c) { System.out.println( "request(" + count + ", " + c + ") BEGIN" ); // (1) 建立FutureData的實體 final FutureData future = new FutureData(); // (2) 為了建立RealData的實體,啟動新的線程 new Thread() { public void run() { //在匿名內部類中使用count、future、c。 RealData realdata = new RealData(count, c); future.setRealData(realdata); } }.start(); System.out.println( "request(" + count + ", " + c + ") END" ); // (3) 取回FutureData實體,作為傳回值 return future; } } |
host("蛋糕店")在接到請求后,先生成了“提貨單”FutureData的實例future,然后命令“蛋糕師傅”RealData去做蛋糕,realdata相當于起個線程去做蛋糕了。然后host返回給顧客的僅僅是“提貨單”future,而不是蛋糕。當蛋糕做好后,蛋糕師傅才能給對應的“提貨單”蛋糕,也就是future.setRealData(realdata)。
下面來看看蛋糕師傅是怎么做蛋糕的:
建立一個字符串,包含count個c字符,為了表現出犯法需要花費一些時間,使用了sleep。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
public class RealData implements Data { private final String content; public RealData( int count, char c) { System.out.println( "making RealData(" + count + ", " + c + ") BEGIN" ); char [] buffer = new char [count]; for ( int i = 0 ; i < count; i++) { buffer[i] = c; try { Thread.sleep( 1000 ); } catch (InterruptedException e) { } } System.out.println( "making RealData(" + count + ", " + c + ") END" ); this .content = new String(buffer); } public String getContent() { return content; } } |
現在來看看“提貨單”future是怎么與蛋糕"content"對應的:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
public class FutureData implements Data { private RealData realdata = null ; private boolean ready = false ; public synchronized void setRealData(RealData realdata) { if (ready) { return ; // 防止setRealData被調用兩次以上。 } this .realdata = realdata; this .ready = true ; notifyAll(); } public synchronized String getContent() { while (!ready) { try { wait(); } catch (InterruptedException e) { } } return realdata.getContent(); } } |
顧客做完自己的事情后,會拿著自己的“提貨單”來取蛋糕:
1
|
System.out.println( "data1 = " + data1.getContent()); |
這時候如果蛋糕沒做好,就只好等了:
1
2
3
4
5
6
7
|
while (!ready) { try { wait(); } catch (InterruptedException e) { } //等做好后才能取到 return realdata.getContent(); |
程序分析
對于每個請求,host都會生成一個線程,這個線程負責生成顧客需要的“蛋糕”。在等待一段時間以后,如果蛋糕還沒有做好,顧客還必須等待。直到“蛋糕被做好”,也就是future.setRealData(realdata); 執行以后,顧客才能拿走蛋糕。
每個線程只是專門負責制作特定顧客所需要的“蛋糕”。也就是顧客A對應著蛋糕師傅A,顧客B對應著蛋糕師傅B。即使顧客B的蛋糕被先做好了,顧客A也只能等待蛋糕師傅A把蛋糕做好。換句話說,顧客之間沒有競爭關系。
類FutureData的兩個方法被設置為synchronized,實際上蛋糕師傅A與顧客A之間的互斥關系,也就是顧客A必須等待蛋糕師傅A把蛋糕做好后,才能拿走,而與蛋糕師傅B是否做好了蛋糕沒有關系。
本文內容就到此全部結束了,代碼簡單吧,希望對大家學習Java多線程實現異步調用有所幫助,謝謝。