ExecutorTest.java
/*
* Copyright@ 2011 www.egtry.com
*/
package egtry.thread;
import java.util.concurrent.Executor;
public class ExecutorTest implements Executor {
private boolean runSyn=true;
public static void main(String[] args) {
System.out.println("Main thread: "+Thread.currentThread().getId());
ExecutorTest executor=new ExecutorTest();
Worker worker1=new Worker();
executor.execute(worker1);
Worker worker2=new Worker();
executor.runSyn=false;
executor.execute(worker2);
System.out.println();
}
@Override
public void execute(Runnable command) {
if (runSyn) {
command.run();
} else {
new Thread(command).start();
}
}
public static class Worker implements Runnable {
@Override
public void run() {
System.out.println("Running in the thread: "+Thread.currentThread().getId());
}
}
}