import tcl.lang.*;

public class Try {
    boolean threadShouldMakeCall;
    Interp theInterp = new Interp();

    private class TstThread extends Thread {
        public synchronized void run() {
            if (threadShouldMakeCall) {
                System.out.println("\tthread: about to do makeTclCalls()");
                makeTclCalls("TstThread");
            }
            try{
                System.out.println("\tthread: about to wait");
                wait();
            } catch (Throwable e) {
                System.out.println("\tthread: got " +e.toString());
            }
            System.out.println("\tthread after wait");
        }
    }

    public void doThrCalls() {
        threadShouldMakeCall = true;
        doit();
    }

    public void doNoThrCalls() {
        threadShouldMakeCall = false;
        doit();
    }

    public void doit() {
        makeTclCalls("mainBefore");
	(new TstThread()).start();
        System.out.println("\tmain after thread start");
        makeTclCalls("mainAfter");
    }

    public synchronized void makeTclCalls(String fromWhere) {
        try {
            theInterp.eval("set x 37");
            System.out.println("\tmakeTclCalls(" +fromWhere + "): value is "
                               +theInterp.getVar("x", null, 0).toString()
            );
        } catch (Throwable e) {
            System.out.println("\tmakeTclCalls(" +fromWhere + "): caught " + e.toString());
        }
    }

}
