Knowledge

Asynchronous method invocation

Source đź“ť

96:
programming environments this requires many lines of code, especially if care is taken to avoid the overhead that may be caused by creating many threads. AMI solves this problem in that it augments a potentially long-running ("synchronous") object method with an "asynchronous" variant that returns immediately, along with additional methods that make it easy to receive notification of completion, or to wait for completion at a later time.
95:
from which it is invoked. If the method takes a long time to complete, e.g. because it is loading data over the internet, the calling thread is blocked until the method has finished. When this is not desired, it is possible to start a "worker thread" and invoke the method from there. In most
338:. A superficial difference is that in this style the return value of the long-running method is passed directly to the callback method. Much more importantly, the API uses a special mechanism to run the callback method (which resides in an event object of type 346:
was called. This eliminates the danger of race conditions, making the API easier to use and suitable for software components; on the other hand this implementation of the pattern comes with additional object creation and synchronization overhead.
326:
to obtain the return value of the long-running method. A problem with the callback mechanism is that the callback function is naturally executed in the worker thread (rather than in the original calling thread), which may cause race conditions.
330:
In the .NET Framework documentation, the term event-based asynchronous pattern refers to an alternative API style (available since .NET 2.0) using a method named
504: 279:
interface), so it can continue the calling thread with unrelated work. In the simplest case, eventually there is no more such work, and the client calls
61:
while waiting for the called code to finish. Instead, the calling thread is notified when the reply arrives. Polling for a reply is an undesired option.
17: 147:
to solve the same problem. This pattern is a variant of AMI whose implementation carries more overhead, but it is useful for objects representing
107:. An example for an application that may make use of AMI is a web browser that needs to display a web page even before all images are loaded. 682: 363: 861: 783: 753: 660: 630: 600: 287:
object normally provides at least a method that allows the client to query whether the long-running method has already completed:
451: 430: 148: 1386: 283:(passing the previously received object), which blocks until the method has completed and the result is available. The 712:
Christian Nagel; Bill Evjen; Jay Glynn; Karli Watson & Morgan Skinner (2008). "Event-based Asynchronous Pattern".
1499: 1411: 825: 729: 144: 77: 1359: 1184: 983: 508: 1207: 1167: 854: 1494: 1177: 1172: 115: 1452: 1290: 140: 123: 1067: 1275: 1270: 1097: 111: 92: 85: 81: 31: 490: 1315: 1280: 1247: 897: 847: 571: 550: 529: 834: 690: 371: 1217: 1189: 1127: 1112: 1092: 1028: 870: 721: 73: 54: 1194: 1122: 1072: 907: 817: 808:
Chris Sells & Ian Griffiths (2007). "Appendix C.3: The Event-Based Asynchronous Pattern".
1473: 1376: 1222: 1202: 1147: 477: 399: 809: 713: 1285: 1242: 1237: 1227: 1137: 104: 58: 34: 88:. It is equivalent to the IOU ("I owe you") pattern described in 1996 by Allan Vermeulen. 8: 1325: 1310: 1305: 1162: 1047: 993: 775: 745: 652: 622: 592: 1447: 1426: 1335: 1232: 1082: 975: 927: 889: 714: 447: 1117: 960: 950: 945: 917: 912: 821: 810: 725: 426: 91:
In most programming languages a called method is executed synchronously, i.e. in the
1416: 1157: 1102: 1023: 1013: 1003: 998: 458: 1406: 1352: 1330: 1107: 1062: 1033: 1008: 988: 935: 902: 878: 1391: 1252: 955: 940: 176: 711: 1488: 1212: 1057: 1018: 965: 160:
Asynchronous Programming Model (APM) pattern (used before .NET Framework 2.0)
100: 1468: 1431: 1320: 1295: 1087: 839: 322:, to be invoked when the long-running method completes. It typically calls 175:
The following example is loosely based on a standard AMI style used in the
1421: 1396: 1381: 1300: 1132: 1401: 746:"Multithreaded Programming with the Event-based Asynchronous Pattern" 163:
Event-based Asynchronous Pattern (EAP) (used in .NET Framework 2.0)
103:
design pattern. Alternatives are synchronous method invocation and
653:"Using an AsyncCallback Delegate to End an Asynchronous Operation" 166:
Task-based Asynchronous Pattern (TAP) (used in .NET Framework 4.0)
776:"Deciding When to Implement the Event-based Asynchronous Pattern" 394: 807: 445: 271:, the client immediately receives an object of type 1486: 675: 356: 855: 869: 439: 551:"Event-based Asynchronous Pattern Overview" 862: 848: 593:"Asynchronous Programming Design Patterns" 705: 392: 768: 738: 645: 615: 585: 318:One can also pass a callback method to 80:invocation of potentially long-running 14: 1487: 421:Nash, Trey (2007). "Threading in C#". 843: 835:Using asynchronous method calls in C# 786:from the original on 22 November 2008 756:from the original on 25 December 2008 663:from the original on 23 December 2008 603:from the original on 22 November 2008 633:from the original on 7 December 2008 420: 623:"Asynchronous Programming Overview" 24: 18:The Task-based Asynchronous Pattern 801: 129: 25: 1511: 572:"Task-based Asynchronous Pattern" 154: 780:.NET Framework Developer's Guide 750:.NET Framework Developer's Guide 687:Distributed Programming with Ice 657:.NET Framework Developer's Guide 627:.NET Framework Developer's Guide 597:.NET Framework Developer's Guide 530:"Asynchronous Programming Model" 395:"An Asynchronous Design Pattern" 368:Distributed Programming with Ice 364:"Asynchronous Method Invocation" 99:One common use of AMI is in the 1360:Enterprise Integration Patterns 782:. Microsoft Developer Network. 752:. Microsoft Developer Network. 659:. Microsoft Developer Network. 629:. Microsoft Developer Network. 599:. Microsoft Developer Network. 564: 543: 522: 507:. Oracle. 2011. Archived from 497: 414: 393:Vermeulen, Allan (June 1996). 386: 342:) in the same thread in which 120:asynchronous method invocation 57:in which the call site is not 39:asynchronous method invocation 13: 1: 350: 134: 64: 689:. ZeroC, Inc. Archived from 370:. ZeroC, Inc. Archived from 7: 1453:Portland Pattern Repository 183:, one adds two new methods 124:asynchronous procedure call 10: 1516: 170: 1461: 1440: 1369: 1344: 1261: 1146: 1046: 974: 926: 888: 877: 47:asynchronous method calls 1500:Software design patterns 1078:Event-based asynchronous 871:Software design patterns 289: 193: 984:Chain of responsibility 27:Software design pattern 1123:Scheduled-task pattern 1073:Double-checked locking 485:Cite journal requires 275:(which implements the 1474:Architectural pattern 1377:Christopher Alexander 816:. O'Reilly. pp.  139:FutureTask class in 122:is a special case of 114:is a special case of 1286:Dependency injection 1243:Inversion of control 1238:Data transfer object 1138:Thread-local storage 716:Professional C# 2008 683:"Concurrency Issues" 51:asynchronous pattern 35:computer programming 1495:Threads (computing) 1291:Intercepting filter 446:Lavender, R. Greg; 423:Accelerated C# 2008 340:AccomplishCompleted 149:software components 93:thread of execution 1448:The Hillside Group 1233:Data access object 1083:Guarded suspension 1068:Binding properties 720:. Wiley. pp.  505:"Class FutureTask" 448:Douglas C. Schmidt 400:Dr. Dobb's Journal 1482: 1481: 1276:Business delegate 1208:Publish–subscribe 1042: 1041: 574:. Microsoft. 2015 553:. Microsoft. 2015 532:. Microsoft. 2015 432:978-1-59059-873-3 374:on 5 January 2008 179:. Given a method 45:), also known as 16:(Redirected from 1507: 1281:Composite entity 1158:Front controller 898:Abstract factory 886: 885: 864: 857: 850: 841: 840: 831: 815: 796: 795: 793: 791: 772: 766: 765: 763: 761: 742: 736: 735: 719: 709: 703: 702: 700: 698: 693:on 28 March 2008 679: 673: 672: 670: 668: 649: 643: 642: 640: 638: 619: 613: 612: 610: 608: 589: 583: 582: 580: 579: 568: 562: 561: 559: 558: 547: 541: 540: 538: 537: 526: 520: 519: 517: 516: 501: 495: 494: 488: 483: 481: 473: 471: 469: 463: 457:. Archived from 456: 443: 437: 436: 418: 412: 411: 409: 407: 390: 384: 383: 381: 379: 360: 345: 341: 337: 333: 325: 321: 314: 311: 308: 305: 302: 299: 296: 293: 286: 282: 278: 274: 270: 263: 260: 257: 254: 251: 248: 245: 242: 239: 236: 233: 230: 227: 224: 221: 218: 215: 212: 209: 206: 203: 200: 197: 190: 186: 182: 21: 1515: 1514: 1510: 1509: 1508: 1506: 1505: 1504: 1485: 1484: 1483: 1478: 1457: 1436: 1427:Douglas Schmidt 1407:Ward Cunningham 1365: 1353:Design Patterns 1340: 1331:Method chaining 1263: 1257: 1218:Service locator 1149: 1142: 1113:Read–write lock 1049: 1038: 1029:Template method 970: 922: 880: 873: 868: 828: 812:Programming WPF 804: 802:Further reading 799: 789: 787: 774: 773: 769: 759: 757: 744: 743: 739: 732: 710: 706: 696: 694: 681: 680: 676: 666: 664: 651: 650: 646: 636: 634: 621: 620: 616: 606: 604: 591: 590: 586: 577: 575: 570: 569: 565: 556: 554: 549: 548: 544: 535: 533: 528: 527: 523: 514: 512: 503: 502: 498: 486: 484: 475: 474: 467: 465: 461: 454: 452:"Active Object" 444: 440: 433: 419: 415: 405: 403: 391: 387: 377: 375: 362: 361: 357: 353: 344:BeginAccomplish 343: 339: 336:BeginAccomplish 335: 332:AccomplishAsync 331: 323: 320:BeginAccomplish 319: 316: 315: 312: 309: 306: 303: 300: 297: 294: 291: 284: 280: 276: 272: 269:BeginAccomplish 268: 265: 264: 261: 258: 255: 252: 249: 246: 243: 240: 237: 234: 231: 228: 226:BeginAccomplish 225: 222: 219: 216: 213: 210: 207: 204: 201: 198: 195: 188: 185:BeginAccomplish 184: 180: 173: 157: 137: 132: 130:Implementations 67: 28: 23: 22: 15: 12: 11: 5: 1513: 1503: 1502: 1497: 1480: 1479: 1477: 1476: 1471: 1465: 1463: 1459: 1458: 1456: 1455: 1450: 1444: 1442: 1438: 1437: 1435: 1434: 1429: 1424: 1419: 1414: 1409: 1404: 1399: 1394: 1392:John Vlissides 1389: 1384: 1379: 1373: 1371: 1367: 1366: 1364: 1363: 1356: 1348: 1346: 1342: 1341: 1339: 1338: 1333: 1328: 1323: 1318: 1313: 1308: 1303: 1298: 1293: 1288: 1283: 1278: 1273: 1267: 1265: 1259: 1258: 1256: 1255: 1250: 1245: 1240: 1235: 1230: 1225: 1220: 1215: 1210: 1205: 1200: 1192: 1187: 1182: 1181: 1180: 1175: 1165: 1160: 1154: 1152: 1144: 1143: 1141: 1140: 1135: 1130: 1125: 1120: 1115: 1110: 1105: 1100: 1095: 1090: 1085: 1080: 1075: 1070: 1065: 1060: 1054: 1052: 1044: 1043: 1040: 1039: 1037: 1036: 1031: 1026: 1021: 1016: 1011: 1006: 1001: 996: 991: 986: 980: 978: 972: 971: 969: 968: 963: 958: 953: 948: 943: 938: 932: 930: 924: 923: 921: 920: 915: 910: 908:Factory method 905: 900: 894: 892: 883: 875: 874: 867: 866: 859: 852: 844: 838: 837: 832: 826: 803: 800: 798: 797: 767: 737: 730: 704: 674: 644: 614: 584: 563: 542: 521: 496: 487:|journal= 438: 431: 413: 385: 354: 352: 349: 290: 194: 177:.NET Framework 172: 169: 168: 167: 164: 161: 156: 155:.NET Framework 153: 136: 133: 131: 128: 105:future objects 74:design pattern 66: 63: 55:design pattern 26: 9: 6: 4: 3: 2: 1512: 1501: 1498: 1496: 1493: 1492: 1490: 1475: 1472: 1470: 1467: 1466: 1464: 1460: 1454: 1451: 1449: 1446: 1445: 1443: 1439: 1433: 1430: 1428: 1425: 1423: 1420: 1418: 1417:Robert Martin 1415: 1413: 1412:Martin Fowler 1410: 1408: 1405: 1403: 1400: 1398: 1395: 1393: 1390: 1388: 1387:Ralph Johnson 1385: 1383: 1380: 1378: 1375: 1374: 1372: 1368: 1362: 1361: 1357: 1355: 1354: 1350: 1349: 1347: 1343: 1337: 1334: 1332: 1329: 1327: 1324: 1322: 1319: 1317: 1314: 1312: 1309: 1307: 1304: 1302: 1299: 1297: 1294: 1292: 1289: 1287: 1284: 1282: 1279: 1277: 1274: 1272: 1269: 1268: 1266: 1260: 1254: 1251: 1249: 1246: 1244: 1241: 1239: 1236: 1234: 1231: 1229: 1226: 1224: 1223:Active record 1221: 1219: 1216: 1214: 1213:Naked objects 1211: 1209: 1206: 1204: 1203:Specification 1201: 1199: 1197: 1193: 1191: 1188: 1186: 1183: 1179: 1176: 1174: 1171: 1170: 1169: 1166: 1164: 1161: 1159: 1156: 1155: 1153: 1151: 1148:Architectural 1145: 1139: 1136: 1134: 1131: 1129: 1126: 1124: 1121: 1119: 1116: 1114: 1111: 1109: 1106: 1104: 1101: 1099: 1096: 1094: 1091: 1089: 1086: 1084: 1081: 1079: 1076: 1074: 1071: 1069: 1066: 1064: 1061: 1059: 1058:Active object 1056: 1055: 1053: 1051: 1045: 1035: 1032: 1030: 1027: 1025: 1022: 1020: 1017: 1015: 1012: 1010: 1007: 1005: 1002: 1000: 997: 995: 992: 990: 987: 985: 982: 981: 979: 977: 973: 967: 964: 962: 959: 957: 954: 952: 949: 947: 944: 942: 939: 937: 934: 933: 931: 929: 925: 919: 916: 914: 911: 909: 906: 904: 901: 899: 896: 895: 893: 891: 887: 884: 882: 876: 872: 865: 860: 858: 853: 851: 846: 845: 842: 836: 833: 829: 827:9780596510374 823: 819: 814: 813: 806: 805: 785: 781: 777: 771: 755: 751: 747: 741: 733: 731:9780470191378 727: 723: 718: 717: 708: 692: 688: 684: 678: 662: 658: 654: 648: 632: 628: 624: 618: 602: 598: 594: 588: 573: 567: 552: 546: 531: 525: 511:on 2013-06-25 510: 506: 500: 492: 479: 464:on 2012-07-22 460: 453: 449: 442: 434: 428: 424: 417: 402: 401: 396: 389: 373: 369: 365: 359: 355: 348: 328: 324:EndAccomplish 288: 281:EndAccomplish 267:Upon calling 244:EndAccomplish 192: 189:EndAccomplish 178: 165: 162: 159: 158: 152: 150: 146: 142: 127: 125: 121: 117: 113: 108: 106: 102: 101:active object 97: 94: 89: 87: 83: 79: 75: 71: 62: 60: 56: 52: 48: 44: 40: 36: 33: 32:multithreaded 19: 1469:Anti-pattern 1432:Linda Rising 1358: 1351: 1296:Lazy loading 1228:Identity map 1195: 1077: 879:Gang of Four 811: 788:. Retrieved 779: 770: 758:. Retrieved 749: 740: 715: 707: 695:. Retrieved 691:the original 686: 677: 665:. Retrieved 656: 647: 635:. Retrieved 626: 617: 605:. Retrieved 596: 587: 576:. Retrieved 566: 555:. Retrieved 545: 534:. Retrieved 524: 513:. Retrieved 509:the original 499: 478:cite journal 466:. Retrieved 459:the original 441: 422: 416: 404:. Retrieved 398: 388: 376:. Retrieved 372:the original 367: 358: 329: 317: 304:HasCompleted 295:IAsyncResult 277:IAsyncResult 266: 250:IAsyncResult 223:IAsyncResult 174: 138: 119: 109: 98: 90: 78:asynchronous 69: 68: 50: 46: 42: 38: 29: 1441:Communities 1422:Jim Coplien 1397:Grady Booch 1382:Erich Gamma 1326:Type tunnel 1311:Object pool 1306:Null object 1301:Mock object 1163:Interceptor 1133:Thread pool 1048:Concurrency 994:Interpreter 790:22 November 760:22 November 697:22 November 667:22 November 637:22 November 607:22 November 468:22 November 406:22 November 378:22 November 334:instead of 285:AsyncResult 273:AsyncResult 1489:Categories 1336:Delegation 1271:Blackboard 976:Behavioral 928:Structural 890:Creational 578:2015-06-29 557:2015-06-29 536:2015-06-29 515:2015-06-29 425:. Apress. 351:References 208:Accomplish 181:Accomplish 135:Java class 65:Background 1402:Kent Beck 1128:Semaphore 1118:Scheduler 961:Flyweight 951:Decorator 946:Composite 918:Singleton 913:Prototype 292:interface 116:procedure 1462:See also 1264:patterns 1150:patterns 1103:Proactor 1050:patterns 1024:Strategy 1014:Observer 1004:Mediator 999:Iterator 881:patterns 784:Archived 754:Archived 661:Archived 631:Archived 601:Archived 1316:Servant 1248:Model 2 1108:Reactor 1098:Monitor 1063:Balking 1034:Visitor 1009:Memento 989:Command 936:Adapter 903:Builder 199:Example 171:Example 82:methods 59:blocked 49:or the 1370:People 1253:Broker 956:Facade 941:Bridge 824:  820:–749. 728:  724:–571. 429:  241:Result 205:Result 145:events 112:method 110:Since 86:object 84:of an 1345:Books 1262:Other 1198:-tier 1019:State 966:Proxy 462:(PDF) 455:(PDF) 196:class 72:is a 53:is a 1321:Twin 1178:MVVM 1093:Lock 1088:Join 822:ISBN 792:2008 762:2008 726:ISBN 699:2008 669:2008 639:2008 609:2008 491:help 470:2008 427:ISBN 408:2008 380:2008 301:bool 232:args 214:args 187:and 143:use 141:Java 76:for 1190:ECS 1185:ADR 1173:MVP 1168:MVC 818:747 722:570 70:AMI 43:AMI 30:In 1491:: 778:. 748:. 685:. 655:. 625:. 595:. 482:: 480:}} 476:{{ 450:. 397:. 366:. 307:() 191:: 151:. 126:. 118:, 37:, 1196:n 863:e 856:t 849:v 830:. 794:. 764:. 734:. 701:. 671:. 641:. 611:. 581:. 560:. 539:. 518:. 493:) 489:( 472:. 435:. 410:. 382:. 313:} 310:… 298:{ 262:} 259:… 256:) 253:a 247:( 238:) 235:… 229:( 220:) 217:… 211:( 202:{ 41:( 20:)

Index

The Task-based Asynchronous Pattern
multithreaded
computer programming
design pattern
blocked
design pattern
asynchronous
methods
object
thread of execution
active object
future objects
method
procedure
asynchronous procedure call
Java
events
software components
.NET Framework
"Asynchronous Method Invocation"
the original
"An Asynchronous Design Pattern"
Dr. Dobb's Journal
ISBN
978-1-59059-873-3
Douglas C. Schmidt
"Active Object"
the original
cite journal
help

Text is available under the Creative Commons Attribution-ShareAlike License. Additional terms may apply.

↑