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
Q43: a(n. _ object is used to control
Q49: Assume two threads share a BankAccount object
Q50: A waiting thread is blocked until another
Q52: Consider the following change to the deposit
Q53: Assume two threads share a BankAccount object
Q55: The _ method is called by a
Q56: Examine the SharedData class shown below. Suppose
Q57: Examine the SharedData class shown below. Suppose
Q58: If a thread sleeps after acquiring a
Q58: Examine the SharedData class shown below. Suppose
Unlock this Answer For Free Now!
View this answer and more for free by performing one of the following actions
Scan the QR code to install the App and get 2 free unlocks
Unlock quizzes for free by uploading documents