
COP4600 Practice Exam 2
1. When calling a synchronized method, what is the method required to own and
how many threads can own it?
a. The lock and a single thread.
2. What are the two conditions for a lock being released?
a. When the holder exits the synchronized method
b. During a wait() call
3. Explain the major difference between notify() and notifyAll().
a. Notify() randomly selects a process on the wait set and moves it to the
entry set.
b. NotifyAll() moves all processes on the wait() set to the entry set.
4. What 3 criteria must be ensured in order to use notify() over notifyAll().
a. All threads in the wait set must be waiting on the same condition.
b. At least one thread must benefit from the condition being met.
c. All sub-classes of the waiting type must ensure that a and b are true.
5. Is this a good product class for testing the Producer-Consumer problem?
Class Item {
Static private into nextSerialNumber = 0;
Private int serialNumber;
Public Item() {
serialNumber = nextSerialNumber++;
}
public String toString() {
return “Item # “ + serialNumber;
}
}
a. not a good product class for testing because nextSerialNumber++ is not
atomic. It’s a read – change – store. Thus, nextSerialNumber++ takes 3
clock cycles and if it is interrupted after the first read problems will arise.
6. What are two types of Voluntary Rescheduling?
a. Yield()
b. Sleep()
7. Which code segment terminates naturally?
a. Public void start() {
setAlive(true);
for(int i = 2; i < 100; i++)
x = x * i;
setAlive(false);
{