Anda di halaman 1dari 19

University of Tartu

Rein Raudjärv

Blocking Calls in Java


Bachelor Thesis (4 AP)

Supervisor: Jevgeni Kabanov

Tartu, May 29th 2007


Basis

„ Idea: 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.

„ Implementation is based on:


Commons Javaflow library
Outline

1. Without Blocking Calls


2. With Blocking Calls
3. API
4. Byte Code Transformation
1. No Blocking Calls
Event Listeners

btn.addActionListener(new ActionListener() {
void actionPerformed(ActionEvent e) {
System.out.println(“Hello world!");
}
});
Event Listeners (2)

btn.addActionListener(new ActionListener() {
int counter;
void actionPerformed(ActionEvent e) {
if (++counter == 10)
System.exit(0);
}
});
2. Blocking Calls
No Event Listeners

SwingBlockingUtil.waitForAction(btn);
System.out.println("Clicked!");
No Event Listeners (2)

for (int i = 1; i <= 10; i++)


SwingBlockingUtil.waitForAction(btn);
System.exit(0);
3. API
Continuation-Passing-Style

interface IndirectCallable {
void call(ReturnContext returnCtx);
}

interface ReturnContext {
void returnWith(Object result);
void failWith(Throwable t);
}
Waiting for Action

void waitForAction(JButton btn) {


BlockingUtil.call(new IndirectCallable() {
void call(ReturnContext returnCtx) {

btn.addActionListener(new ActionListener() {
void actionPerformed(ActionEvent e) {
returnCtx.returnWith(null);
}
});

}
});
}
Executing a Blocking Call

@Blocking
void myBlockingMethod() {
String myVar = myBlockingCall();
...
}

String myBlockingCall() {
IndirectCallable ic = new IndirectCallable() {
void call(ReturnContext returnCtx) {
...
returnCtx.returnWith("myValue");
}
};
return (String) BlockingUtil.call(ic);
}
4. Byte Code Transformation
Continuations Transformer

„ At running:
– Trace execution state as Java object
„ For suspending:
– Return if current method should suspend
„ At resuming:
– Jump to the point that was executed last

(Commons Javaflow library)


Blocking Transformer

@Blocking
void myBlockingMethod() {
...
}

void myBlockingMethod() {
BlockingUtil.execute(new Runnable() {
void run() {
...
}
});
}
Conclusions

Event-based Æ sequencial programming


Low-level Æ high level programming

+ Shorter code - Longer byte code


+ Less coupling - Slower byte code
Outline of the Thesis

1. Java Bytecode – needed to understand


transformations, based on ASM User
Guide
2. Continuations – describes concept of
continuation and Javaflow library
3. Blocking Calls – describes concept of
blocking call, API, implementation and
Java agent developed in this work
4. Case Study – two examples
Thank You! Questions?

http://www.ut.ee/~reinra/blocking/

Anda mungkin juga menyukai