Source Code For:
"Designing Objects for Concurrency in Java", part 1
Vol. 2, Issue 4, p. 22

Listing 1
/* FILE: Outer.java */
/** Lexical scoping sample */

class Supper { int x = 0; }

public class Outer extends Supper {
protected int x;
protected Inner innerObj;
private String outerStr = " Outer val: ";

Outer(int x) {
this.x = x;
innerObj = this.new Inner(x + 1);
}

protected class Inner {
protected int x;
protected String innerStr = " Inner val: ";

Inner(int x) { this.x = x; }

public String toString() {
return ((Supper)Outer.this).x +
outerStr + Outer.this.x +
innerStr + x;
}
}

public String toString() {
return innerObj.toString();
}

public static void main(String args[]) {
Outer o = new Outer(1);
System.out.println("Test Supper: " + o);
}
}
//Result: Test Supper: 0 Outer val: 1 Inner val: 2

/* How it works ? The bytecode might look like:
public class Outer {
//...
class Inner {
private Outer this$0;//saved copy of Outer.this
Outer$Inner(Outer this$0) {//Inner's ctr.
this.this$0 = this$0;
}
public void method() {//Outer's method
Inner innerObj = new Outer$Inner(this);
*/

Listing 2
abstract class Operator implements Runnable {
//message obj for callback client
private Runnable success, failure;
//type of operand inner class
abstract protected class Operand { }
abstract protected boolean operatorImp();
protected synchronized void operator()
{ if (operatorImp()) success.run(); ... }
public void run() { operator(); }

Listing 3
abstract class Operation {
//action in case of success
abstract protected void successfulOperation();
//inner class as Runnable
protected class Success implements Runnable {
public void run() { successfulOperation(); }
}
abstract public void start();
}

Listing 4
/* FILE: Show.java */
import java.net.*;
import java.io.*;
/** Model for an Unary Operator class */
abstract class Operator implements Runnable {
private Runnable success;
private Runnable failure;

/** Operand: an abstract protected inner class */
abstract protected class Operand { }

public Operator(Runnable s, Runnable f) {
success = s; failure = f;
}

// placeholder for operator implementation
abstract protected boolean operatorImpl();

protected synchronized void operator() {
if (operatorImpl()) success.run();
else failure.run();
}
public void run() { operator(); }
}

/** Sample of a server: Operator called Show */
public class Show extends Operator {
DefOperand someURL; //operator's operand
protected class DefOperand
extends Operator.Operand {
protected boolean result = true;
protected URL url;
//constructor for inner class
public DefOperand(String urlString) {
try {
url = new URL(urlString);
} catch (MalformedURLException e) {
result = false;
}
}
}
public Show(String str, Runnable s, Runnable f){
super(s, f);
//for this operator define an new operand
someURL = this.new DefOperand(str);
}
//particular operator's method implementation
protected boolean operatorImpl() {
String line;
if (someURL.result) {
try {
URLConnection c =
someURL.url.openConnection();
DataInputStream dis =
new DataInputStream(c.getInputStream());
//method readLine has been deprecated: J1.1
while ((line = dis.readLine()) != null)
System.out.println(line);
dis.close();
} catch (IOException ioe) {
someURL.result = false;
}
}
return someURL.result;
}
}

/* FILE: ShowURL.java */
/** Model for a Waiter Pattern: Operation class */
abstract class Operation {

abstract protected void succesfulOperation();
abstract protected void failedOperation();

protected class Success implements Runnable {
public void run() {succesfulOperation();}
}

protected class Failure implements Runnable {
public void run() {failedOperation();}
}
abstract public void start();
}

/** Sample of Operation called ShowURL */
public class ShowURL extends Operation {
protected String urlString;
public ShowURL(String s) { urlString = s; }

protected void succesfulOperation() {
System.out.println("Success: " + urlString);
}
protected void failedOperation() {
System.out.println("Failure: " + urlString);
}

public void start() {
Runnable s = new Success();
Runnable f = new Failure();
new Thread(new Show(urlString, s, f)).start();
}

public static void main(String args[]) {
String url = "http://www.javadevelopersjournal.com";
new ShowURL(url).start();
}
}
// Result: ... html Success: http://www.javadevelopersjournal.com

Listing 5
public class ShowURL extends Operation {
...
public void start() {
Runnable s = new Success();
Runnable f = new Failure();
new Thread(new Show(urlString, s, f)).start();
}