Closures in Java
November 4th, 2006
This is a hint abount implementing closures in Java. Of course, if you want to do it in an acceptable way, you would create a custom interface that Does The Right Thing™, something like (semi pseudocode)
interface Closure {
public Object foo(Object bar);
}
However, you can get some ideas from the sequent code
{
protected String x ;
{
x = "foo";
}
{
final String y = "yy";
Thread t = new Thread(){
{
x = "boo" + y;
}
};
return t;
}
{
StateModifier sm = new StateModifier();
Thread t1 = sm.getThread();
System.out.println(sm.x);
t1.start();
try {
t1.join();
} catch (Exception e){
// ...
}
System.out.println(sm.x);
System.exit(0);
}
}
Leave a Reply