UNIVERSITY OF TARTU FACULTY OF MATHEMATICS AND COMPUTER SCIENCE Institute of Computer Science

Rein Raudjärv

Blocking Calls in Java Bachelor thesis (4 AP)

Supervisor: Jevgeni Kabanov

Autor: ………………………………………

“….” mai

2007

Juhendaja: …………………………………..

“….” mai

2007

TARTU 2007

2

Contents Introduction .........................................................................................................................4 1. Java Bytecode ..................................................................................................................6 1.1. Class Structure..........................................................................................................6 1.2. Execution Model.......................................................................................................7 1.3. Bytecode Instructions ...............................................................................................7 1.4. Bytecode Transformation .........................................................................................8 2. Continuations.................................................................................................................10 2.1. Continuation ...........................................................................................................10 2.2. Continuation in Java ...............................................................................................11 2.3. Javaflow API ..........................................................................................................11 2.4. Javaflow Implementation .......................................................................................11 2.5. Bytecode Transformation .......................................................................................13 3. Blocking Calls ...............................................................................................................15 3.1. Blocking Methods...................................................................................................15 3.2. Blocking Calls Core API ........................................................................................16 3.3. Swing Blocking API...............................................................................................18 3.4. Aranea Blocking API..............................................................................................18 3.5. Blocking Calls Implementation ..............................................................................19 3.6. Blocking Annotation...............................................................................................20 3.7. Bytecode Transformation .......................................................................................21 4. Case Study .....................................................................................................................23 4.1. Planning a Party......................................................................................................23 4.2. Click 10 Times........................................................................................................25 Conclusions .......................................................................................................................28 Summary (in Estonian)......................................................................................................29 Bibliography ......................................................................................................................30

3

Introduction This chapter introduces the work, its goal, its scope and its target audience.

Motivation Long ago only sequential programming was used to create simple MS-DOS-based applications. Nowadays desktop and web applications both are developed using eventbased approach. The latter is very common due to multi-threading and networking but it makes the code more complicated. The event based programming can be supplemented by the old sequential style using continuations. In Smalltalk this was demonstrated in Seaside Framework [DL04]. In Java [GJ05] continuations were first introduced in RIFE [Be]. However the implementation was framework dependent and later Commons Javaflow [Cu] provided a more general solution. The main goal of this work is to go further with Javaflow continuations by implementing blocking calls. Simply this is calling methods that may return later. We hope that providing sequential APIs for today’s event based libraries, we make the programming easier as well as bring new opportunities into programming.

Prerequisites The thesis implies the knowledge of basic programming in Java. It also implies the understanding of observer pattern that is used in event-based libraries such as Java Swing [ST]. For understanding the case study and some parts of implementation some knowledge of Aranea Web Framework [Ka] is needed. For a quick introduction see the introductory tutorial [Ka07].

Contributions and Outline The first chapter introduces Java bytecode and instrumentation introduced in Java 5.0. It is based on a user guide [Bru07]. This is needed to understand the bytecode transformations described in latter chapters.

4

The second chapter describes the concept of continuation and gives an overview of Commons Javaflow library that implements continuations in Java. The third chapter is the main part of the work. It describes the concept of blocking call, the API, the implementation based on Javaflow library and a Java instrumentation agent developed in this work. The forth and final chapter provide two examples of how blocking calls can improve programming. The examples are based on Aranea Web Framework and Java Swing library. The main contribution is the blocking calls library including the Java instrumentation agent. Also an example application is provided that compares blocking approach with classic event-based programming. The distribution is available on the accompanying CD and from the website http://www.ut.ee/~reinra/blocking/.

5

Chapter 1 Java Bytecode This chapter is based on the ASM User Guide [Br07] and gives a basic overview of Java bytecode and how it is executed. For a thorough reference consult JVM specification [LY99]. We also cover how we can redefine existing classes and load new ones during runtime. This chapter is needed to understand the bytecode transformations described in latter chapters. Although we will use ASM bytecode manipulation library [BL02] to do the actual bytecode transformation, we do not cover its API and will describe only the conceptual transformation.

1.1. Class Structure The central concept of Java code is class. Figure 1 summaries the overall structure of a compiled class. Modifiers, name, super class, interfaces Constant pool: numeric, string and type constants Source file name (optional) Enclosing class reference Annotation* Attribute* Inner class* Name Field* Modifiers, name, type Annotation* Attribute* Method* Modifiers, name, return and parameter types Annotation* Attribute* Compiled code Figure 1: Overall structure of a compiled class (* means zero or more)

6

Inside compiled classes the code of methods is stored as a sequence of bytecode instructions.

1.2. Execution Model Before presenting the bytecode instructions it is necessary to present the Java Virtual Machine execution model. As you know Java code is executed inside threads. Each thread has its own execution stack, which is made of frames. Each frame represents a method invocation: each time a method is invoked, a new frame is pushed on the current thread’s execution stack. When the method returns, either normally or because of an exception, this frame is popped from the execution stack and execution continues in the calling method (whose frame is now on top of the stack). Each frame contains two parts: a local variables part and an operand stack part. The local variables part contains variables that can be accessed by their index, in random order. The operand stack part, as its name implies, is a stack of values that are used as operands by bytecode instructions. This means that the values in this stack can only be accessed in Last In First Out order. Do not confuse the operand stack and the thread’s execution stack: each frame in the execution stack contains its own operand stack.

1.3. Bytecode Instructions A bytecode instruction is made of an opcode that identifies this instruction, and of a fixed number of arguments: The opcode is an unsigned byte value – hence the bytecode name – and is identified by a mnemonic symbol. For example the opcode value 0 is designed by the mnemonic symbol NOP, and corresponds to the instruction that does nothing. The arguments are static values that define the precise instruction behavior. They are given just after the opcode. For instance the GOTO label instruction, whose opcode value is 167, takes as argument label, a label that designates the next instruction to be executed. Instruction arguments must not be confused with instruction operands: argument values are statically known and are stored in the compiled code, while operand values come from the operand stack and are known only at runtime. The bytecode instructions can be divided in two categories: •

A small set of instructions is designed to transfer values from the local variables to the operand stack, and vice versa;

7



The other instructions only act on the operand stack: they pop some values from the stack, compute a result based on these values, and push it back on the stack.

The ILOAD, LLOAD, FLOAD, DLOAD, and ALOAD instructions read a local variable and push its value on the operand stack. Symmetrically the ISTORE, LSTORE, FSTORE, DSTORE and ASTORE instructions pop a value from the operand stack and store it in a local variable. The xLOAD and xSTORE instructions are typed (in fact, almost all instructions are typed). All other bytecode instructions work on the operand stack only. They can be grouped in the following categories: Stack instructions are used to manipulate values on the stack. Constants instructions push a constant value on the operand stack. Arithmetic and logic instructions correspond to the +, -, *, /, %, operations etc. Casts instructions correspond to cast expressions in Java. Objects instructions are used to create objects, lock them, test their type, etc. Fields instructions read or write the value of a field. Methods instructions invoke a method or a constructor. Arrays instructions are used to read and write values in arrays. Jumps instructions jump to an arbitrary instruction if some condition is true, or unconditionally. They are used to compile if, for, do, while, break, continue and switch instructions. Return instructions are used to terminate the execution of a method and to return its result to the caller.

1.4. Bytecode Transformation Prior to Java 5.0 in order to modify class bytecode one had to use a custom class loader or transform the generated class-files in the file system. Java 5.0 introduces a new way – instrumentation. A good introduction can be found in [Lo05]. In short, instrumentation allows us to add any number of transformers modifying classes that are just defined by the class loader. A transformer is a filter for array of bytes containing a class. Previously, only one transformer could be implemented by using its own class loader.

8

In addition to transforming existing classes, there is also a need to generate new ones. This cannot be achieved by instrumentation because we cannot split a class in two. New classes are made available for JVM by calling one of the non-public methods of a class loader, e.g.: protected ClassLoader.defineClass(String name, byte[] b, int off, int len).

This method cannot be called outside the java.lang package unless using Reflection API and disabling security checks. Inevitably this restricts using blocking calls only as long as the SecurityManager allows it.

9

Chapter 2 Continuations This chapter describes the concept of continuation and Commons Javaflow library [JF] implementing it in Java.

2.1. Continuation A continuation can be thought as a snapshot of the program running state that can later be restored as if nothing happened. Many languages provide constructs for saving execution into and later restoring it. In general, a continuation is represented by a function that takes one argument and calling this function causes the program to continue from a certain point, returning the passed value as the function result. The following example illustrates continuation use in Haskell [JS03]: foo :: Int -> Cont r String foo n = callCC $ \k -> do let n' = n ^ 2 + 3 when (n' > 20) $ k "over twenty" return (show $ n' - 4)

Here, k is a continuation that can be thought of as the 'return' statement which immediately exits the function. The function callCC is generally refered to as call-withcurrent-continuation function (commonly abbreviated call/cc) [HF84]. Alternatively we can do without modifications to the language by making every function take a continuation function that will be passed the resulting value. Such approach is called Continuation-Passing-Style (CPS) [Re74].

10

2.2. Continuation in Java In Java a continuation must store the stack which includes local variables and the program counter. In fact, we do not generally need to save the whole stack but just a part of it starting from a fixed method call – this is called a partial continuation. There is no native support for continuations in Java. Luckily partial continuations are implemented by Commons Javaflow library. This does the following: •

Provide methods for starting, suspending and resuming continuations.



Transforms the bytecode enabling Java stack to be stored and restored.

Suspending of a partial continuation and later resuming it using Javaflow is performed as follows: Runnable myRunnable = new Runnable() { void run() { ... Continuation.suspend(); ... } }; Continuation cont1 = Continuation.startWith(myRunnable); ... Continuation cont2 = Continuation.continueWith(cont1);

In the following section we cover the Javaflow API in details.

2.3. Javaflow API The central class in Javaflow is Continuation which has the following methods: class Continuation { static Object getContext() static static static static static

Continuation Continuation Continuation Continuation Continuation

static static static static

void void void void

startSuspendedWith(Runnable pTarget) startWith(Runnable pTarget) startWith(Runnable pTarget, Object pCtx) continueWith(Continuation pOldCont) continueWith(Continuation pOldCont, Object pCtx)

suspend() exit() again() cancel()

}

The Continuation class has no public constructors. The only way to get an instance is to use one of the start() or continue() methods. The start() method starts a new Continuation and continue() resumes the existing one. The pCtx parameter is just an

11

arbitrary object, which is then made available to the Runnable by calling Continuation.getContext().

All other methods can only be used by the Runnable that is passed into a start() or continue() method. They are used as follows:



suspend() stops the current Continuation so that start() or continue()

methods return a new Continuation that corresponds to the stopping point. •

exit() stops the current Continuation so that start() or continue() meth-

ods return null to disable resuming this Continuation. •

cancel() stops the current Continuation so that start() or continue()

methods return the same Continuation that was passed to them in order to rerun it. •

again() stops the current Continuation and re-runs it from the last stop point

immediately.

2.4. Javaflow Implementation Executing a Continuation causes the Java stack to be duplicated as a normal Java object that can later be resumed. An execution of method is tracked down as its operand stack which is represented by class Stack. This class reflects exactly the JVM operand stack. This is possible due to the Java bytecode transformation which tracks the stack push and pop methods calls inside each class each method. It also adds alternate start-points for resuming each method. The continuation behavior is added by Stack subclass StackRecorder which has the following fields and methods: class StackRecorder extends Stack { static ThreadLocal threadMap = new ThreadLocal(); boolean isRestoring; boolean isCapturing; private Object context; StackRecorder(Runnable pTarget) StackRecorder(Stack pParent) static void suspend() StackRecorder execute(Object context) Object getContext() static StackRecorder get() }

12

The fields and methods are used as follows: •

threadMap

holds

StackRecorder

instances

that

are

accessible

by

StackRecorder.get().



context just holds a value that was passed in and made available inside a Continuation by calling getContext().



isRestoring

represents whether the method is being resumed and

isCapturing whether the method is being suspended. Both of these are called

from the transformed bytecode. •

A method execution is started by calling execute() and suspended by calling suspend().

There is one special exception used to implement the continuation logic – ContinuationDeath is thrown inside a Continuation by Continuation.exit(), Continuation.cancel() and Continuation.again(). This is handled by method Continuation.continueWith(). Thus, the exception is hidden outside the Javaflow API.

2.5. Bytecode Transformation Commons Javaflow provides two options to modify the compiled Java classes. 1. Using an appropriate Ant task (non-runtime). 2. Using a custom class loader (runtime). Later, we will also provide a Java agent to deal with it. All of these are based on a transformer that we are covering next.

Javaflow Transformer Commons Javaflow transformer enables us to use the continuations API. The authors of framework planned to provide an implementation based on both BCEL (The Byte Code Engineering Library) [Da01] and ASM, but at the moment of writing this thesis the latter is still incomplete. The Javaflow transformer does the follows: 1. Inserts instructions to keep track of the state of each method – each operation on the operand stack is duplicated on the thread-local instance of Stack. 2. Inserts instructions to check if current method should suspend. This is indicated by StackRecorder.get().isCapturing == true.

13

3. Inserts instructions to start each method from the „check-point” inside the method. As frequently as the method is being checked for suspending, there is an option to continue from the same position. These positions are marked by labels and there are instructions inserted at the beginning that check which label to jump to. This means that although technically each method is still started from the beginning when resumed, the already executed parts are skipped. The condition that current method is just being resumed, is marked by StackRecorder.get().isRestoring == true.

14

Chapter 3 Blocking Calls This chapter describes the main contribution of this thesis, including the concept, the API and the implementation and Java instrumentation of the blocking calls defined in [MK06].

3.1. Blocking Methods A method is called blocking if it is suspendable and later resumable by itself. Inside a blocking method, one can use blocking calls to other methods suspending the first method and resuming it any time. The effect is similar to partial continuations provided by Javaflow except that the blocking method (a Runnable passed to the method Continuation.startWith()) has control to resume itself later. This distinction makes it possible to reuse blocking calls and hide the low level API from the developer. Using the Javaflow API, we should write in addition to the each Runnable corresponding control logic of resuming it. A blocking call can be executed, suspended and resumed (possibly returning a value) as follows: BlockingRunnable myRunnable = new BlockingRunnable() { void run() { IndirectCallable myCallable = new IndirectCallable() { void call(ReturnContext returnContext) { ... returnContext.returnWith("myValue"); } }; String myVar = (String) BlockingUtil.call(myCallable); ... } }; BlockingUtil.execute(myRunnable);

15

The actual code for developer looks simpler: @Blocking void myBlockingMethod() { String myVar = myBlockingCall(); ... }

In the next section, we cover the blocking calls API in details.

3.2. Blocking Calls Core API In this section, we describe the core API of blocking calls

Executing Blocking Method As we are using partial continuations from Commons Javaflow framework we have to distinct the blocking method from other parts of code. The blocking method is represented by BlockingRunnable interface: interface BlockingRunnable extends Serializable { void run(); }

Because the entire stack of the blocking method is tracked it must be serializable. NB! Not only the BlockingRunnable interface is serializable, but each local variable in method run() must be serializable. To execute a blocking method, one must call BlockingRunnable runnable = new BlockingRunnable() { void run() { ... } }; BlockingUtil.execute(runnable);

Calling BlockingUtil.execute(BlockingRunnable) does two things: 1. Executes the blocking method with Continuation tracking down its entire stack and enabling the method to suspend. 2. Makes the suspended method resumable. In Java, the current thread executes the blocking method until it suspends or finishes – technically the method just returns. The blocking method must have a void return type since there is no way to return a meaningful value from a suspended method (which we still technically have to do).

16

Suspending Blocking Method As mentioned earlier we can also suspend and resume plain Javaflow continuations, but that is not reusable because the resuming logic is contained outside the continuation itself. By running a blocking call we do not just suspend the blocking method. We also provide a new „runnable” that is executed as the blocking method suspends. This „runnable” has the control to resume the blocking method. In order to suspend and resume a blocking method, we must do the following: IndirectCallable myCallable = new IndirectCallable() { void call(ReturnContext returnContext) { ... returnContext.returnWith("myValue"); } }; String myVar = (String) BlockingUtil.call(myCallable); BlockingUtil.call() does the follows:

1. Suspends the blocking method. 2. Executes the provided IndirectCallable. 3. When returnContext.returnWith("myValue") is called the blocking method resumes by assigning "myValue" to myVar. The IndirectCallable and ReturnContext have the following methods. interface IndirectCallable { void call(ReturnContext returnContext); } interface ReturnContext { void returnWith(Object result); void failWith(Throwable t); }

The IndirectCallable is just an abstraction of a usual method that has the explicit control over returning a value or throwing an exception. This explicitness enables us to use the ReturnContext from an arbitrary method, usually an event listener which is a powerful way to hide the event-based programming. In fact the ReturnContext can be made to resume the blocking method more than once providing new opportunities to our usual programming style. But the most important thing is that blocking calls can be reused forming new APIs that we cover in the following sections.

17

3.3. Swing Blocking API For Swing library, at the moment following methods are provided for suspending a blocking method: class SwingBlockingUtil { static void waitForAnyAction(AbstractButton static void waitForOneAction(AbstractButton static void waitForAnyChange(AbstractButton static void waitForOneChange(AbstractButton }

button) button) button) button)

waitFor*Action() and waitFor*Change() provide the Button with an ActionListener or ChangeListener respectively.

“Any” corresponds to the fact that the blocking method is resumed from this point each time the respective listener is called. In case of “One” the listener is called only once. The usual method that abstracts the event listening looks the following: static void waitForAnyAction(AbstractButton button) { BlockingUtil.call(new IndirectCallable() { void call(ReturnContext returnCtx) { ActionListener listener = new ActionListener() { void actionPerformed(ActionEvent e) { returnCtx.returnWith(null); } }; button.addActionListener(listener); } }); }

The blocking method is suspended by calling BlockingUtil.call() and is resumed if the ActionListener added to the specified Button is reached. The main fact here is that the ReturnContext is passed to another method. There is nothing magical about suspending and new methods can be introduced just as easily.

3.4. Aranea Blocking API For Aranea Web Framework, currently the following methods are provided for suspending a blocking method. class AraneaBlockingUtil { static Object call(FlowContext flowCtx, Widget flow, Configurator configurator) throws FlowCancelException static void wait(BaseApplicationWidget widget, String eventId) static void waitForClick(ButtonControl control) static void waitForChange(DateTimeControl control) static void waitForChange(StringArrayRequestControl control) }

18

These methods do the follows: •

The method call() calls the FlowContext.start() and returns when the subflow ends execution. If the subflow is cancelled, a FlowCancelException is thrown.



wait() adds an EventListener to the Widget and returns when it is called.



waitForClick() adds an OnClickEventListener to the Control and returns

when it is called. •

waitForChange() adds an OnChangeEventListener to the Control and re-

turns when it is called.

3.5. Blocking Calls Implementation Blocking calls are based on Commons Javaflow continuations. In addition the implementation is similar to that in two ways. 1. There is only one class that should be accessed from outside (BlockingUtil). 2. There is a thread-local class (BlockingHelper) for internal use. BlockingUtil just gets a thread local instance of the BlockingHelper and proxies

the call to method with the same name. The BlockingHelper is based on Javaflow API as follows: class BlockingHelper { Continuator continuator; void execute(BlockingRunnable runnable) { // Create the initial Continuation Continuation c = Continuation.startSuspendedWith(runnable); // Start it continueWith(c, null); } void continueWith(Continuation initCont, Object ctx) { // Continue Continuation Continuation newCont = Continuation.continueWith(initCont, ctx); // Continuation finished or suspended if (newCont == null) { // Continuation finished return; } // Continuation suspended // Start Continuator and provide it with SuspendContext this.continuator.run(new Continuator.SuspendContext() { void doContinue(Object ctx) { // Continue the current Continuation continueWith(newCont, ctx); } }); }

19

Object call(IndirectCallable callable) { // Define continuator this.continuator = new Continuator() { void run(SuspendContext ctx) { callable.call(new ReturnContext() { void returnWith(Object result) { ctx.doContinue(new Object[] {result}); } void failWith(Throwable t) { ctx.doContinue(t); } }); } }; // Let's sleep! Continuation.suspend(); // Get context! Object ctx = Continuation.getContext(); if (ctx instanceof Object[]) { // Unwrap and return value return ((Object[]) ctx)[0]; } // Uncheck and throw exception throw ExceptionUtil.uncheck((Throwable) ctx); } }

Let’s examine each field and method: •

execute() is called only the first time blocking method is started. It creates

the new Continuation, doesn’t start it, passes it the BlockingRunnable to and calls continueWith(). •

continueWith() is called as executing a blocking method first time as con-

tinuing

it

later.

This

can

be

seen

as

a

wrapper

method

for

Continuation.continueWith(). If the latter returns null, blocking method

has finished and this method returns also. Otherwise this.continuator provided by the last call to the method call() is run. It is provided with SuspendContext that calls the continueWith() method recursively if the

method SuspendContext.ReturnContext.doContinue() is called. •

call() suspends an already running blocking method performing a blocking

call. This can be seen as a wrapper to Continuation.suspend(). Before suspending the continuation, this.continuator is provided with a new Continuator that wraps the IndirectCallable provided to the method call(). After the current blocking method is resumed the data is read back and

value is retuned or an exception is thrown respectively.

20

Altogether blocking calls improve Commons Javaflow continuations with two features: 1. A continuation can suspend providing a callback that is able to resume this continuation. 2. Suspending the continuation has the syntax of a normal method call (returning values as well as throwing exceptions are supported). In the following sections we show how the BlockingUtil.execute() can be hidden also.

3.6. Blocking Annotation Instead of the following syntax: void myMethod() { BlockingUtil.execute(new BlockingRunnable() { void run() { ... } }); }

In Java 5 and later, there is a much simpler approach: @Blocking void myMethod() { ... }

A method that is marked @Blocking must have return type void. In the next section, we show how this annotation works.

3.7. Bytecode Transformation In this section we describe bytecode transformation used in blocking calls.

Blocking Agent Aranea blocking calls contain a Java instrumentation agent that adds two class file transformers. 1. Commons Javaflow transformer (described on page 13). 2. Blocking transformer. These transformers are filtered so that only packages set in the agent options are included. Otherwise the instrumentation would take very long as well as cause validation errors due to different Java versions, incorrect order of class loading etc. 21

Blocking Transformer As

we

described

blocking

behavior

can

be

achieved

by

calling

BlockingUtil.execute(). Blocking transformer enables us to use the @Blocking

annotation which simplifies the programming letting the code to remain clear of the continuation or blocking-specific API. The blocking transformer is based on the ASM Java bytecode manipulation framework. In addition to transforming existing classes, it also generates new ones and defines them as described in section 1.4. Blocking transformer does the following: 1. Finds a method marked with the @Blocking annotation; 2. Removes the annotation (to not transform any class twice); 3. Examines the method marked @Blocking and adds to this class a new public accessor method for each non-public field and method; 4. Creates a new inner class that implements BlockingRunnable. This class has a constructor that takes all arguments of the method marked @Blocking as well as the instance of the outer class. All of these arguments are assigned to the corresponding generated instance fields. The run() method contains the original blocking method body. Each access to a method argument is transformed to an access to an inner class instance field. Each access to a non-public field or method is transformed to an access to an accessor method; 5. Replaces the blocking method body with bytecode creating a new instance of the generated inner class and passing it to the BlockingUtil.execute() call. As the method marked @Blocking must correspond to the void BlockingRunnable.run()

it must have a void return type.

22

Chapter 4 Case Study 4.1. Planning a Party In this section we investigate a simple wizard-like flow in Aranea Web Framework. We first provide it an event-based solution and then compare it with a blocking implementation.

Problem Consider the following case. We want to plan a party going through the following steps: 1. Set a title of the party. 2. Choose a location. 3. Set the start and end time. 4. Choose people to invite. 5. Send invitations.

Event-based Implementation We create a flow container and start a new flow for each step. When starting a flow we also pass the flow container a handler. Each handler processes the returned value and starts the next flow. The disadvantage is that we couple processing values and starting the following flows together. This complicates making changes to the actual order.

23

The following Widget implements the event-based approach for our problem: class EventBasedPartyWidget extends BaseUIWidget { ... void planParty() { final Party party = new Party(); final Handler peopleHandler = new Handler() { void execute(Object persons) { party.setPersons((List) persons); getFlowCtx().start(new InvitationsWidget(party); } }; ... final Handler titleHandler = new Handler() { void execute(Object title) { party.setTitle((String) title); getFlowCtx().start(new LocationWidget(), locationHandler)); } }; getFlowCtx().start(new TitleWidget(), titleHandler); } }

Now, let’s describe the method planParty(). First we define a local variable party that will contain all the information gathered later. Then we define new handlers as anonymous inner classes assigned to new local variables. Since our local variables are referenced from the method level inner classes, we must define them as final. This also enforces us to define them in strict order which is exactly the opposite of how the program is executed. Each handler has two actions. Firstly, it collects the returned value and updates the variable party. Secondly, it starts the next flow by passing the flow context a new Widget and next Handler. At the end of the planParty() the first flow is started exactly the same way.

Blocking Implementation Instead of passing these event handlers with each new flow it would be easier to just call a method and wait for its return value. This is exactly what blocking calls do.

24

The following Widget implements the blocking way for our problem: class BlockingPartyWidget extends BaseUIWidget { ... @Blocking void planParty() { try { Party party = new Party(); party.setTitle(chooseTitle()); party.setLocation(chooseLocation()); party.setTime(chooseTime()); party.setPersons(choosePersons()); sendInvitations(party); } catch (FlowCancelException e) { // just cancel the current flow as finishing normally } getFlowCtx().cancel(); } String chooseTitle() { return (String) AraneaBlockingUtil.call(getFlowCtx(), new TitleWidget()); } ... void sendInvitations(Party party) { AraneaBlockingUtil.call(getFlowCtx(), new InvitationsWidget(party)); } }

Now we have method planParty() annotated as @Blocking. This enables us to use blocking calls by calling AraneaBlockingUtil.call() inside the planParty(). We create a new Party object, assign its fields values from the following methods, and send invitations. At last we finish the current flow returning to the previous page. Notice that this implementation is shorter and simpler than the event-based approach. We do not have any inner classes or final variables. Moreover we could easily change the order in which the different flows are called by switching the corresponding lines in method planParty(). In short, we can concentrate on the actual program instead of complicated language structures. The only disadvantage is that blocking calls in Java need bytecode instrumentation that makes the transformed classes larger and slower.

4.2. Click 10 Times In this section we examine a simple Swing frame that waits until a button is clicked 10 times and then exists. We look at an event-based and a blocking implementation.

25

Event-based Implementation The following implementation uses the classical event-based approach: class ClassicSwingExample extends JPanel { void init() { JButton button = new JButton("Click on me!"); add(button); button.addActionListener(new ActionListener() { int counter; void actionPerformed(ActionEvent e) { if ((++counter == 10) { System.exit(0); } } }); } public static void main(String[] args) { ClassicSwingExample panel = new ClassicSwingExample(); JFrame frame = new JFrame(); frame.add(panel); panel.init(); } }

In method init() we add a button with a listener that is called when we click on the button. This listener has its own state containing the number of clicks. On each click, this is increased and when reaching to 10, the program exists.

Blocking Implementation The following implementation uses the blocking approach: class BlockingSwingExample extends JPanel { @Blocking void init() { JButton button = new JButton("Click on me!"); add(button); for (int i = 1; i <= 10; i++) { SwingBlockingUtil.waitForOneAction(button); } System.exit(0); } public static void main(String[] args) { BlockingSwingExample panel = new BlockingSwingExample(); JFrame frame = new JFrame(); frame.add(panel); panel.init(); } }

26

Instead of adding a listener we wait for 10 clicks and then exit. Calling a method SwingBlockingUtil.waitForOneAction() waits for one click on the specified button.

Although SwingBlockingUtil still uses listeners internally we have the choice whether to use them explicitly or not. Since we could not use a for in the first case it demonstrates that blocking calls let us use sequential programming where it was usually impossible.

27

Conclusions The main goal of this work was to implement blocking calls in Java using an existing library – Commons Javaflow – for continuations in Java. We implemented the blocking calls core API, provided some Aranea and Swing dependent blocking calls and also implemented a Java bytecode instrumentation agent that enabled us to use the @Blocking annotation. In Aranea framework, the blocking calls let us call a flow as calling a normal Java method. We showed an example of combining different flows and how this can be implemented in an event-based way and by a blocking approach. The latter made it possible to uncouple the dependencies that were otherwise hardly avoidable. Therefore, we could easily change the order of flow calls or re-use them. In a word, the code was easier to read and maintain. We also showed an example of how the blocking approach enables us to use a for-cycle where it was else impossible. Altogether blocking approach does not replace the event-based programming, but hides the low-level part and lets us express the application logic using higher-level approach. We can still use the event-based way where appropriate. At the same combining event-based and sequential programming together is a very powerful idea because different parts of application logic can be expressed using the most suitable tool.

28

Java blokeeruvad kutsed Rein Raudjärv Bakalaureusetöö Kokkuvõte Käesoleva töö põhieesmärk on realiseerida Java blokeeruvad meetodikutsed kasutades olemasolevat Commons Javaflow teeki Java jätkude jaoks. Me realiseerime blokeeruvate kutsete põhitoe, lisame mõned Aranea ja Swing raamistikes kasutatavad kutsed ning samuti Java baitkoodi teisendaja, mis võimaldab kasutada @Blocking annotatsiooni. Blokeeruvad kutsed lubavad Aranea raamistikus kutsuda uut voogu nagu tavalist Java meetodit. Võrdleme sündmuspõhist ja blokeeruvate kutsetega voogude kasutamist näite varal, millest selgub, et teisel juhul esineb koodis vähem sõltuvusi, me saame kergesti muuta voogude järjekorda ning neid taaskasutada. Ühesõnaga, sellisel kujul kood on kergemini loetav ja hallatav. Lisaks vaatleme näidet, kus saame blokeeruvate kutsete abil kasutada for-tsüklit, kus see muidu on võimatu. Kokkuvõttes, blokeeruvate meetodikutsete kasutamine ei asenda sündmuspõhist programmeerimist kuid aitab meil programmiloogikat esitada kõrgemal tasemel. Võime alati kasutada sündmuspõhist lähenemist, kus rohkem sobilik, kuid kombineerides mõlemat nii sündmuspõhist kui järjestikprogrammeerimist, saame väljendada erinevaid programmi loogika osi vastavalt kõige sobivama vahendiga.

29

Bibliography [BL02]

E. Bruneton, R. Lenglet, T. Coupaye. ASM – a code manipulation tool to implement adaptable systems. In Adaptable and Extensible Component Systems, Grenoble, France, 2002.

[Br07]

E. Bruneton et al. ASM 3.0 – A Java bytecode engineering library User Guide. 2007. http://download.forge.objectweb.org/asm/asm-guide.pdf (30.04.2007)

[Cu]

T. Curdt et al. Commons Javaflow. http://jakarta.apache.org/commons/sandbox/javaflow/ (20.05.2007)

[Da01]

M. Dahm. Bytecode Engineering with the BCEL API. Java Informationstage, 99, pages 267-277. 2001.

[DL04]

S. Ducasse, A. Lienhard, L. Renggli. Seaside – a multiple control flow web application framework. ESUG 2004 Research Track, pages 231-257, 2004.

[GJ05]

J. Gosling, B. Joy, G. Steele, G. Bracha. JavaTM Language Specification, The (3rd Edition) (The Java Series). Prentice Hall PTR, 2005. http://java.sun.com/docs/books/jls/ (18.05.2007)

[HF84]

C. T. Haynes, D. P. Friedman, M. Wand. Continuations and coroutines. In Proceedings of the 1984 ACM Symposium on LISP and functional programming, 293-298, ACM Press New York, NY, USA, 1984.

[JS03]

S. P. Jones. Haskell 98 Language and Libraries: The Revised Report. Cambridge University Press, 2003.

[Ka07]

J. Kabanov. Aranea Introductory Tutorial. 2007 http://www.araneaframework.org/docs/intro/html/ (20.05.2007)

[Ka]

J. Kabanov et al. Aranea Web Framework http://www.araneaframework.org/ (20.05.2007)

[LY99]

T. Lindholm, F. Yellin. The JavaTM Virtual Machine Specification (2nd Edition). Prentice Hall PTR, 1999. http://java.sun.com/docs/books/jvms/ (18.05.2007)

30

[Lo05]

R. J. Lorimer. Instrumentation: Modify Applications with Java 5 Class File Transformations. 2005. http://www.javalobby.org/java/forums/t19309.html (20.05.2007)

[MK06] O. Mürk, J. Kabanov. Aranea: web framework construction and integration kit. In Proceedings of the 4th international symposium on Principles and practice of programming in Java, 163-172, ACM Press New York, NY, USA, 2006. [Re74]

J. C. Reynolds. On the Relation between Direct and Continuation Semantics. Proceedings of the 2nd Colloquium on Automata, Languages and Programming, 141-156, Springer-Verlag London, UK, 1974.

[ST]

The Swing Tutorial. http://java.sun.com/docs/books/tutorial/uiswing/ (20.05.2007)

31

Blocking Calls in Java - Semantic Scholar

FACULTY OF MATHEMATICS AND COMPUTER SCIENCE. Institute of Computer Science. Rein Raudjärv. Blocking Calls in Java. Bachelor thesis (4 AP).

276KB Sizes 1 Downloads 541 Views

Recommend Documents

Blocking Calls in Java - Semantic Scholar
Chapter 3. Blocking Calls. This chapter describes the main contribution of this ... since there is no way to return a meaningful value from a suspended method ...

Efficient Spectral Neighborhood Blocking for Entity ... - Semantic Scholar
106, no. 50, pp. 21 068–21 073, 2009. [23] G. Salton, A. Wong, and C. S. Yang, “A vector space model for automatic indexing,” Communications of the ACM, vol. 18, no. 11, pp. 613–620, 1975. [24] P. McNamee and J. Mayfield, “Character п-gram

in chickpea - Semantic Scholar
Email :[email protected] exploitation of ... 1990) are simple and fast and have been employed widely for ... template DNA (10 ng/ l). Touchdown PCR.

in chickpea - Semantic Scholar
(USDA-ARS ,Washington state university,. Pullman ... products from ×California,USA,Sequi-GenGT) .... Table 1. List of polymorphic microsatellite markers. S.No.

Networks in Finance - Semantic Scholar
Mar 10, 2008 - two questions arise: how resilient financial networks are to ... which the various patterns of connections can be described and analyzed in a meaningful ... literature in finance that uses network theory and suggests a number of areas

Discretion in Hiring - Semantic Scholar
In its marketing materials, our data firm emphasizes the ability of its job test to reduce ...... of Intermediaries in Online Hiring, mimeo London School of Economics.

Distinctiveness in chromosomal behaviour in ... - Semantic Scholar
Marathwada Agricultural University,. Parbhani ... Uni, bi and multivalent were 33.33%, 54.21 % and. 2.23 % respectively. Average ... Stain tech, 44 (3) : 117-122.

Distinctiveness in chromosomal behaviour in ... - Semantic Scholar
Cytological studies in interspecific hybrid derivatives of cotton viz., IS-244/4/1 and IS-181/7/1 obtained in BC1F8 generation of trispecies cross ... Chromosome association of 5.19 I + 8.33 II +1.14III + 1.09IV and 6.0 I+ 7.7 II +0.7III + 1.25IV was

Notio - A Java API for developing CG tools - Semantic Scholar
providing a platform for the development of tools and applications. ... to access the underlying graph implementation (e.g. Deakin Toolset [3] and CGKEE. [4]).

EventJava: An Extension of Java for Event ... - Semantic Scholar
This paper presents EventJava, an extension of Java with generic support for ...... Nystrom, N., Clarkson, M.R., Myers, A.C.: Polyglot: An Extensible Compiler.

EventJava: An Extension of Java for Event ... - Semantic Scholar
highly tuned database-backed event correlation engine as well as to a comparably ... in the Jess expert system shell [10], and the JGroups [11] group commu- nication .... tvReview[5](String model1, File review, float rating) when. (for i in 0..3 ...

Supporting Variable Pedagogical Models in ... - Semantic Scholar
eml.ou.nl/introduction/articles.htm. (13) Greeno, Collins, Resnick. “Cognition and. Learning”, In Handbook of Educational Psychology,. Berliner & Calfee (Eds) ...

Protecting Vulnerable Subjects in Clinical ... - Semantic Scholar
States Department of Health and Human Services. The. Office for Human ... The list of human-subject research abuses in the United. States is regrettably long. ... cal investigators protected vulnerable research subjects by excluding them from ...

OPTIONALITY IN EVALUATING PROSODY ... - Semantic Scholar
ILK / Computational Linguistics and AI. Tilburg, The Netherlands ..... ISCA Tutorial and Research Workshop on Speech Synthesis,. Perthshire, Scotland, 2001.

Deciphering Trends In Mobile Search - Semantic Scholar
Aug 2, 2007 - PDA and computer-based queries, where the average num- ber of words per ... ing the key and the system cycles through the letters in the order they're printed. ... tracted from that 5 seconds to estimate the network latency (the ..... M

Identifying global regulators in transcriptional ... - Semantic Scholar
discussions and, Verónica Jiménez, Edgar Dıaz and Fabiola Sánchez for their computer support. References and recommended reading. Papers of particular interest, .... Ju J, Mitchell T, Peters H III, Haldenwang WG: Sigma factor displacement from RN

integrating fuzzy logic in ontologies - Semantic Scholar
application of ontologies. KAON allows ... cycle”, etc. In order to face these problems the proposed ap- ...... porting application development in the semantic web.

SEVEN CONSECUTIVE PRIMES IN ARITHMETIC ... - Semantic Scholar
A related conjecture is the following: there exist arbitrarily long sequences of consecutive primes in arithmetic progression [2]. In 1967, Lander and Parkin. [4] reported finding the first and smallest sequence of 6 consecutive primes in AP, where t

Modelling Situations in Intelligent Agents - Semantic Scholar
straints on how it operates, or may influence the way that it chooses to achieve its ... We claim the same is true of situations. 1049 ... to be true. Figure 2 depicts the derivation rules for cap- turing when a particular situation becomes active an

Predicting Human Reaching Motion in ... - Semantic Scholar
algorithm that can be tuned through cross validation, however we found the results to be sparse enough without this regularization term (see Section V).

INVESTIGATING LINGUISTIC KNOWLEDGE IN A ... - Semantic Scholar
bel/word n-gram appears in the training data and its type is included, the n-gram is used to form a feature. Type. Description. W unigram word feature. f(wi). WW.

Efficient Semantic Service Discovery in Pervasive ... - Semantic Scholar
computing environments that integrate heterogeneous wireless network technolo- ... Simple Object Access Protocol (SOAP) on top of Internet protocols (HTTP, SMTP). .... In this area, various languages have been proposed to describe.

COMMITTEE CONNECTIVITY IN THE UNITED ... - Semantic Scholar
random graph theory, statistics and computer science, we discuss what con- .... At the start of each two-year term, Representatives deliver written requests to ..... We call this Model A. Figure 4 shows that these fixed degree assignments ...