Solved

Consider the AddFirst Method of the LinkedList Class in Chapter

Question 54

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:

verifed

Verified

Unlock this answer now
Get Access to more Verified Answers free of charge

Related Questions

Unlock this Answer For Free Now!

View this answer and more for free by performing one of the following actions

qr-code

Scan the QR code to install the App and get 2 free unlocks

upload documents

Unlock quizzes for free by uploading documents