Multiple Choice
Consider the addFirst method of the LinkedList class in Chapter 16: /**
Adds an element to the front of the linked list.
@param element the element to add
*/
Public void addFirst(Object element)
{
Node newNode = new Node() ;
NewNode.data = element;
NewNode.next = first;
First = newNode;
}
Three implementations have been proposed to make the addFirst method thread safe where listLock is a variable of type ReentrantLock. Which of them will work?
I.
ListLock.lock() ;
Try
{
Node newNode = new Node() ;
NewNode.data = element;
NewNode.next = first;
}
Finally
{
ListLock.unlock() ;
}
First = newNode;
II.
Node newNode = new Node() ;
NewNode.data = element;
NewNode.next = first;
ListLock.lock() ;
Try
{
First = newNode;
}
Finally
{
ListLock.unlock() ;
}
III.
ListLock.lock() ;
Try
{
Node newNode = new Node() ;
NewNode.data = element;
NewNode.next = first;
First = newNode;
}
Finally
{
ListLock.unlock() ;
}
A) None of them
B) III only
C) II and III only
D) All of them
Correct Answer:

Verified
Correct Answer:
Verified
Q3: The _ method is useful only if
Q36: "Livelock" occurs when one thread runs continuously,
Q37: Examine the SharedData class shown below. Suppose
Q42: When a sleeping thread is interrupted, an
Q43: The Runnable interface includes which method(s)?<br>I public
Q44: When a thread is interrupted, the most
Q44: Examine the SharedData class shown below. Suppose
Q45: Stale data occurs in multi-CPU machines when
Q51: In which method are the tasks that
Q70: The _ method does not actually cause