Friday, September 25, 2009

AutoRunnableDemo

длинный коммент к этому посту

public class AutoRunnableDemo {

static interface AutoRunnable extends Runnable { }

static aspect Runner {
void around(): target(AutoRunnable) && execution(* run(..)) {
(new Thread() { public void run() { proceed(); } }).start();
}
}

static class AutoRunnableImpl implements AutoRunnable {
public void run(){
try{
Thread.currentThread().sleep(1000);
}catch (InterruptedException e) {}

System.out.println("inside AutoRunnableImpl.run() " +
"curentThread name: " +
Thread.currentThread().getName());
}
}

static class RunnableImpl implements Runnable {
public void run(){
System.out.println("inside RunnableImpl.run() " +
"curentThread name: " +
Thread.currentThread().getName());
}
}

public static void main(String[] args){
System.out.println("inside Demo.main(...) curentThread name: " +
Thread.currentThread().getName());

(new AutoRunnableImpl()).run();
(new RunnableImpl()).run();
}
}

компилируется и запускается с помощью (установить aspectj):
ajc -cp aspectjrt.jar AutoRunnableDemo.java
java -cp .:aspectjrt.jar AutoRunnableDemo

наиболее вероятный результат выполнения:
inside Demo.main(...) curentThread name: main
inside RunnableImpl.run() curentThread name: main
inside AutoRunnableImpl.run() curentThread name: Thread-0

0 comments:

Post a Comment