printable version

Test 2

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

Problem X2.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 = 2;

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

    public void doMain() {
        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);
    }
}