Rein Raudjärv
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)
interface IndirectCallable {
void call(ReturnContext returnCtx);
}
interface ReturnContext {
void returnWith(Object result);
void failWith(Throwable t);
}
Waiting for Action
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
@Blocking
void myBlockingMethod() {
...
}
void myBlockingMethod() {
BlockingUtil.execute(new Runnable() {
void run() {
...
}
});
}
Conclusions
http://www.ut.ee/~reinra/blocking/