Week 14 Flashcards
Suppose I have a class MyRunnable that implements the Runnable interface and want to start executing it in a new thread.
new Thread(new Runnable()).start();
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
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.