Kamalakshi N. et al / International Journal of Computer Science and Information Technology (IJCSIT) 2010, Vol1 (1), 1-7

Asynchronous method invocation in Join java and polyphonic c#

Kamalakshi N Dr.H.Naganna Asst Professor, Professor & Head Dept of Computer Science &Engg Dept of Information Science Saptagiri College of Engineering, &Engg SJB Institute of Bangalore INDIA Technology,Bangalore INDIA [email protected] [email protected]

Abstract: In most programming pattern described in 1996 by Allan languages a called method is executed Vermeulen.[2][3]. The event-based synchronously, i.e. in the of asynchronous pattern in .NET execution from which it is invoked. If Framework and the the method needs a long time to java.util.concurrent.FutureTask class in completion, e.g. because it is loading Java use events to solve the same data over the internet, the calling problem. This pattern is a variant of thread is blocked until the method has AMI whose implementation carries more finished. When this is not desired, it is overhead, but it is useful for objects possible to start a "worker thread" and representing software components. invoke the method from there. In most programming environments this One common use of AMI is in the active requires many lines of code, especially object . Alternatives are if care is taken to avoid the overhead synchronous method invocation and that may be caused by creating many future objects[4].An example for an threads. AMI solves this problem in application that may make use of AMI is that it augments a potentially long- a web browser that needs to display a running ("synchronous") object web page even before all images are method with an "asynchronous" loaded. variant that returns immediately, along with additional methods that make it The following are possible reasons for easy to receive notification of defining a method as asynchronous: completion, or to wait for completion at a later time. This paper focuses on The function within the method asynchronous method invocation in two uses the update task - The popular languages Join java and C# . method is terminated by an event generated when the update task is 1 .Introduction successful. If you nevertheless define the method as a In (multithreaded) object-oriented synchronous method under these programming, asynchronous method circumstances, the workflow data invocation (AMI), also known as may be inconsistent with the asynchronous method calls or current data in the database due asynchronous pattern is a design pattern to update terminations or delays. for asynchronous invocation of potentially long-running methods of an The method can also be called object.[1] It is equivalent to the IOU outside the workflow - If it is

1

Kamalakshi N. et al / International Journal of Computer Science and Information Technology (IJCSIT) 2010, Vol1 (1), 1-7

possible that a user may call the  Asynchronous methods function encapsulated in the  Order class modifiers for method outside the workflow determining the order that although the object is also being patterns are matched processed under the control of a workflow at the same time, you Concurrency in most popular should implement the method as programming languages is implemented an asynchronous method. It is using constructs such as semaphores and therefore irrelevant whether monitors. Libraries are emerging (such calling the function takes place as the Java concurrency library JSR-166) inside or outside the workflow that provide higher-level concurrency system. The workflow continues semantics. Communicating Sequential after the event occurs. Processes (CSP), Calculus of Communicating Systems (CCS) and Pi An asynchronous method with have higher-level synchronization comprehensive functionality is behaviours defined implicitly through called in the task - This task gets the composition of events at the its specific character from the interfaces of concurrent processes. Join terminating events. e.g., In a task, calculus, in contrast, has explicit a specific order is to be released synchronization based on a localized for billing. In the task, call the conjunction of events defined as (general) method Process order reduction rules. Join semantics try to and enter the event Billing block provide explicit expressions of deleted as the terminating event. synchronization without breaching the object-oriented idea of modularization, It is also to be possible for the task to be including dynamic creation and terminated by events not necessarily destruction of processes and channels. from method processing - The task in which the method Process sales order is The Join Java language can express executed is also terminated by the event virtually all published concurrency Customer unable to pay patterns without explicit recourse to low- level monitor calls. In general, Join Java 2Join Java programs are more concise than their Java equivalents. The overhead Join Java is a programming language introduced in Join Java by the higher- that extends the standard Java level expressions derived from the Join programming language with the join calculus is manageable. The semantics of the join-calculus. It was synchronization expressions associated written at the University of South with monitors (wait and notify) which Australia within the Reconfigurable are normally located in the body of Computing Lab by Dr. Von Itzstein. methods can be replaced by Join Java expressions (the Join methods) which The Join Java extension introduces three form part of the method signature. new language constructs:

 Join methods

2

Kamalakshi N. et al / International Journal of Computer Science and Information Technology (IJCSIT) 2010, Vol1 (1), 1-7

Join methods Example:

A Join method is defined by two or more class ordered SimpleJoinPattern Join fragments. A Join method will { void A() & B() { execute once all the fragments of the } Join pattern have been called. If the void A() & C() { return type is a standard Java type then } the leading fragment will block the caller void A() & D() { } until the Join pattern is complete and the signal D() & E() { method has executed. If the return type } is of type signal then the leading } fragment will return immediately. All trailing fragments are asynchronous so Asynchronous methods will not block the caller. Asynchronous methods are defined by Example: using the signal return type. This has the same characteristics as the void type class JoinExample { except that the method will return int fragment1() & immediately. When an asynchronous fragment2(int x) { //will return value of x method is called a new thread is created //to caller of fragment1 to execute the body of the method. return x; } Example: } class ThreadExample { Ordering modifiers signal thread(SomeObject x) { //this code will execute Join fragments can be repeated in in a new thread } multiple Join patterns so there can be a } case when multiple Join patterns are completed when a fragment is called. Such a case could occur in the example C Sharp is a multi-paradigm below if B(), C() and D() then A() are programming language that supports called. The final A() fragment completes imperative, generic and object-oriented three of the patterns so there are three programming. It is a part of the possible methods that may be called. Microsoft .NET Framework. It is similar The ordered class modifier is used here to C++ in its object-oriented syntax and to determine which Join method will be is also influenced by Java and Delphi. called. The default and when using the Polyphonic C# extends C#. MC# is an unordered class modifier is to pick one extension of Polyphonic C# that can of the methods at random. With the work on the .NET platform. C-omega is ordered modifier the methods are an extension to C# that succeeded prioritised according to the order they Polyphonic C#. It enables access to data are declared. stores and includes constructs that support concurrent programming.

3

Kamalakshi N. et al / International Journal of Computer Science and Information Technology (IJCSIT) 2010, Vol1 (1), 1-7

3.Polyphonic C# public string Get() & public async Put(string s) { Polyphonic C# adds just two new return s; concepts: asynchronous methods and } chords. } The code above defines a class Buffer Asynchronous Methods. Conventional with two instance methods, which are methods are synchronous, in the sense jointly defined in a single chord. Method that the caller makes no progress until string Get() is a synchronous method the callee completes. In Polyphonic C], taking no arguments and returning a if a method is declared asynchronous string. Method async Put(string s) is then any call to it is guaranteed to asynchronous (so returns no result) and complete essentially immediately. takes a string argument. Asynchronous methods never return a If buff is a instance of Buffer and one result (or throw an exception); they are calls the synchronous method buff .Get() declared by using the async keyword then there are two possibilities: instead of void. Calling an asynchronous —If there has previously been an method is much like sending a message, unmatched call to buff .Put(s) (for some or posting an event.Since asynchronous string s) then there is now a match, so methods have to return immediately, the the pending Put(s) is dequeued and the behaviour of a method such as async body of the chord runs, returning s to the postEvent(EventInfo data) f // large caller of buff .Get(). method body g is the only thing it could —If there are no previous unmatched reasonably be: the call returns calls to buff .Put(.) then the call to buff immediately and ‘large method body’ is .Get() blocks until another thread scheduled for execution in a different supplies a matching Put(.). thread (either a new one spawned to Conversely, on a call to the service this call, or a worker from some asynchronous method buff .Put(s), the pool). However, this kind of definition is caller never waits, but there are two actually rather rare in Polyphonic C]. possible behaviours with regard to other More commonly, asynchronous methods threads: are defined using chords, as described —If there has previously been an below, and do not necessarily require unmatched call to buff .Get() then there new threads. is now a match, so the pending call is dequeued and its associated blocked Chords. A chord (also called a thread is ‘synchronization pattern’, or ‘join awakened to run the body of the chord, pattern’) consists of a header and a body. which returns s. The header is a set of method —If there are no pending calls to buff declarations separated by ‘&’. The body .Get() then the call to buff .Put(s) is is only executed once all the methods in simply queued up until one arrives. the header have been called. Method Exactly which pairs of calls are matched calls are implicitly queued up up is unspecified, so even a single- until/unless there is a matching chord. threaded program such as Consider for example Buffer buff = new Buffer(); public class Buffer { buff .Put(”blue”);

4

Kamalakshi N. et al / International Journal of Computer Science and Information Technology (IJCSIT) 2010, Vol1 (1), 1-7 buff .Put(”sky”); public string Get() & public async Console.Write(buff .Get() + buff .Get()); Put(string s) { is non-deterministic (printing either return s; ”bluesky” or ”skyblue”). } Note that the implementation of Buffer public string Get() & public async does not involve spawning any threads: Put(int n) { whenever the body of the chord runs, it return n.ToString(); does so in a preexisting thread (viz. the } one that called Get()). The reader may at this point wonder what are the rules for } deciding in which thread a body runs, or how we know to which method call the The second new syntax that is enabled in final value computed by the body will be chords is the ability to “join” methods returned. The answer is that in any given with each other. Two or more methods chord, at most one method may be joined with each other are what we refer synchronous. If there is such a method, to as a “chord.” All method calls are then the body runs in the thread kept in a queue, and the body of a chord associated with a call to that method, and is executed when all the joined methods the value is returned to that call. Only if in a chord are present in the queue. The there is no such method (i.e. all the syntax for joining a chord is simply methods in the chord are asynchronous) listing all the methods in the chord does the body run in a new thread, and in separated by an ampersand (“&”). Going that case there is no value to be returned. back to the previous example, It should also be pointed out that the public class ClassUsingAsyncVersion2 Buffer code, trivial though it is, is { threadsafe.The locking that is required public async m1(); (for example to prevent the argument to public async m2(); a single Put being returned to two when m1() & m2() distinct Gets) is generated automatically { by the compiler.More precisely, deciding //method stuff whether any chord is enabled by a call } and, if so, removing the other pending } calls from the queues and the As is the case here, chords may be body for execution is an atomic composed entirely of asynchronous operation. Apart from this atomicity methods. In this case, each call to m1() guarantee, however, there is nomonitor- or m2() completes instantaneously as like mutual exclusion between chord normal and the calling thread continues bodies. Any mutual exclusion that is immediately following the call. Each required must be programmed explicitly object has an underlying queue in which in terms of synchronization conditions in it stores all calls to methods that have chord headers. not been part of a completed chord. The Buffer example uses a single chord When all the methods of a chord are to define two methods. It is also possible found in this queue, and the chord is (and common) to have multiple chords composed of only asynchronous involving a given method. For example: methods as in the example above, the public class Buffer { chord body is not executed in any of

5

Kamalakshi N. et al / International Journal of Computer Science and Information Technology (IJCSIT) 2010, Vol1 (1), 1-7 the calling threads. After all, how would possible chord that could be executed in we decide which one to interrupt? the queue, but the two chords share a Instead, a new thread is created, or an method. For example, in the third available thread is drawn from a thread version of the ClassWithAsync, as seen pool, depending on the implementation. above, m1() is part of two different It is also possible for a chord to return a chords. In the program above, it might value if one of the methods in the chord be the case that there are calls to m2() is synchronous. That is, only one method and m3() already made, and a call to may have a return type other than m1() completes two chords at the same “async.” When a thread makes a call to time. We must decide which chord the synchronous method in a chord, it executes and which must wait for blocks until the chord is completed, at another call to the shared method. In the which point the body of the chord is original Polyphonic C# paper, it is stated executed in the same thread of the that generally this decision will be synchronous method. This is important nondeterministic, i.e. one of them will be to note: a chord that contains a chosen at random, and you should not synchronous method does not result in write your program to rely on a specific the creation of a new thread. Building on behavior. A major subject of this paper the previous example, here is a class is an alternative to this non-determinism with a synchronous chord: that might make chords more useful. public class ClassUsingAsyncVersion3 { 4.CONCLUSION public async m1(); public async m2(); This paper discusses on the overview when m1() & m2() Join Java and Polyphonic C# { //method stuff REFERENCES } public int m3() & m1() [1] "Asynchronous Method { Invocation". Distributed return 0; Programming with Ice. ZeroC, Inc.. } http://www.zeroc.com/doc/Ice- } 3.2.1/manual/Async.34.2.html#711 39. Retrieved 22 November 2008. If a thread makes a call to m3() without [2] Vermeulen, Allan (June 1996). "An any previous calls to m1() by any other Asynchronous Design Pattern". Dr. threads, it Dobb's Journal. will block and wait to be notified. It will http://www.ddj.com/184409898. be notified when another thread makes a Retrieved 22 November 2008. [3] Nash, Trey (2007). "Threading in call to m1() and the chord is complete, C#". Accelerated C# 2008. Apress. and the thread that called m3() will then ISBN 9781590598733. execute the code [4] Lavender, R. Greg; Douglas C. in the body of the chord, in this case Schmidt (PDF). “Active Object:” simply returning “0.” There is one final http://www.cs.wustl.edu/~schmidt/ PDF/Act-Obj.pdf. Retrieved 22 important rule to consider about the November 2008. behavior of chords. We must decide [5] Nick benton, Luca cardelli, and what to do when there is more than one Ce´dric fournet “Modern

6

Kamalakshi N. et al / International Journal of Computer Science and Information Technology (IJCSIT) 2010, Vol1 (1), 1-7

Concurrency Abstractions for C#” [6] Joel Barciauskas “Extensions to Microsoft Research Lab 2004 Polyphonic C# “ 2006

7