nerdexam
Oracle

1Z0-805 · Question #30

Given: public class MyGrades { private final List<Integer> myGrades = new ArrayList<Integer>(); private final ReadWriteLock rwlock = new ReentrantReadWriteLock(); public void addGrade(Integer grade)…

The correct answer is B. rwlock.readLock().lock(). We need a read lock, not a write lock, we are just reading data, not writing/updating data. To aquire and release the lock the method lock() and unlock are used.

Concurrency

Question

Given:

public class MyGrades { private final List<Integer> myGrades = new ArrayList<Integer>(); private final ReadWriteLock rwlock = new ReentrantReadWriteLock(); public void addGrade(Integer grade) { /* lock and modify */ } public void averageGrades() { // acquire _______ lock Line ** double sum = 0; int i = 0; Oracle 1Z0-805 Exam for (i = 0; i < myGrades.size(); i++) { sum += myGrades.get(i); } // release __________ lock Line *** System.out.println("The average is: " + sum/(i+1)); } } Which pair's statements should you insert at line ** and line *** (respectively) to acquire and release the most appropriate lock?

Options

  • Arwlock.readLock().acquire();
  • Brwlock.readLock().lock();
  • Crwlock.getLock().acquire();
  • Drwlock.getLock().lock();
  • Erwlock.WriteLock().acquire();
  • Frwlock.writeLock().lock();

How the community answered

(31 responses)
  • A
    10% (3)
  • B
    84% (26)
  • C
    3% (1)
  • D
    3% (1)

Explanation

We need a read lock, not a write lock, we are just reading data, not writing/updating data. To aquire and release the lock the method lock() and unlock are used.

Topics

#ReentrantReadWriteLock#read lock#write lock#concurrency API

Community Discussion

No community discussion yet for this question.

Full 1Z0-805 Practice