Week 14 Flashcards

1
Q

Suppose I have a class MyRunnable that implements the Runnable interface and want to start executing it in a new thread.

A

new Thread(new Runnable()).start();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

1.public class MyClass {
2. public int global = 0;
3. public int fun(int param) {
4. int local = global;
5. local += param;
6. param *= 2;
7. return local;
}
}

Thread A executes line 5 while thread B executes line 6

Thread A executes line 6 while thread B executes line 6

Thread A executes line 4 while thread B executes line 5

None of the above

A

none. Because “local” is a local variable and “param” is a method parameter, each thread has its own copy, so there is no possibility that one thread can affect the other. The only possible variable on which there could be a race condition is “global,” but it is not modified here.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly