printable version

Quiz 2

[1] [2] [3] [4] [5]

Problem Q2.1.

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

public class ThreadExample extends Thread {
    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 + " " + sub.y);
    }

    private int x = 0;
    private int y = 3;

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

    public void doMain() {
        y = 5;
        x = 2;
    }
}