天天看点

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 =

; 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多线程实现异步调用有所帮助,谢谢。

转载来自 https://www.jb51.net/article/72892.htm