printable version

Test 1

[1] [2] [3] [4] [5] [6] [7] [8] [9]

Problem X1.1.

[12 pts] List all possible outputs for the below program, and explain how each could occur.

public class ThreadExample extends Thread {
    private int x = 0;
    private int y = 1;
    private int z = 2;

    public void run() {
        y = 4;
        x += z;
    }

    public void doMain() {
        z = 8;
        x += y;
    }

    public static void main(String[] argsthrows InterruptedException {
        ThreadExample sub = new ThreadExample();
        sub.start();   // kick off 'run'
        sub.doMain();  // simultaneously do 'doMain'
        sub.join();    // 'doMain' is complete; wait for 'run' to complete
        System.out.println(sub.x);
    }
}