title: What is the best way to simulate java.lang.Thread?
body: <p>I'm developing the transformer for <strong>Java 6<sup>*1)</sup></strong> that performs a kind of <em>partial evaluation</em> but let's consider, for simplicity, <em>abstract-syntax-tree interpretation</em> of a Java program.</p>
  
  <p>How to simulate the <code>Thread</code>'s behavior by an interpreted program?</p>
  
  <p>At the moment I have in mind the following:</p>
  
  <p><em>AstInterpreter</em> should implement <code>java.lang.Runnable</code>. It also should rewrite every new-instance-expression of the <code>java.lang.Thread</code> (or its sub-class) replacing the <code>Thread</code>'s target (<code>java.lang.Runnable</code>) with  the new <em>AstInterpreter</em> instance:</p>
  
  <p><strong>EDIT: more complex examples provided.</strong></p>
  
  <p><strong>EDIT 2: remark 1.</strong></p>
  
  <p>Target program:</p>
  
  <pre><code>class PrintDemo {
     public void printCount(){
      try {
           for(int i = 5; i &gt; 0; i--) {
              System.out.println("Counter   ---   "  + i );
           }
       } catch (Exception e) {
           System.out.println("Thread  interrupted.");
       }
     }
  }
  
  class ThreadDemo extends Thread {
     private Thread t;
     private String threadName;
     PrintDemo  PD;
  
     ThreadDemo( String name,  PrintDemo pd){
         threadName = name;
         PD = pd;
     }
     public void run() {
       synchronized(PD) {
          PD.printCount();
       }
       System.out.println("Thread " +  threadName + " exiting.");
     }
  
     public void start ()
     {
        System.out.println("Starting " +  threadName );
        if (t == null)
        {
           t = new Thread (this, threadName);
           t.start ();
        }
     }
  }
  
  public class TestThread {
     public static void main(String args[]) {
        PrintDemo PD = new PrintDemo();
  
        ThreadDemo T1 = new ThreadDemo( "Thread - 1 ", PD );
        ThreadDemo T2 = new ThreadDemo( "Thread - 2 ", PD );
  
        T1.start();
        T2.start();
  
        // wait for threads to end
        try {
           T1.join();
           T2.join();
        } catch( Exception e) {
           System.out.println("Interrupted");
        }
     }
  }
  </code></pre>
  
  <p>program 1 (ThreadTest - bytecode interpreted):</p>
  
  <pre><code>new Thread( new Runnable() {
     public void run(){
        ThreadTest.main(new String[0]);
     }
  });
  </code></pre>
  
  <p>program 2 (ThreadTest - AST interpreted):</p>
  
  <pre><code>final com.sun.source.tree.Tree tree = parse("ThreadTest.java");
  
  new Thread( new AstInterpreter() {
     public void run(){
        interpret( tree );
     }
  
     public void interpret(com.sun.source.tree.Tree javaExpression){
     //...  
  
     }
  });
  </code></pre>
  
  <p>Does the resulting <strong>program 2</strong> simulate the Thread's behavior of the initial <strong>program 1</strong> correctly?</p>
  
  <p><sup>1)</sup> Currently, <code>source=8 / target=8</code> scheme is accepted. </p>