nerdexam
Oracle

1Z0-805 · Question #76

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) { // acquire _______ lock myGrades.add(grade); // release __________ lock } public void averageGrades() { Oracle 1Z0-805 Exam // acquire _______ lock Line ** double sum = 0; int i = 0; 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 lines ** and lines *** (respectively) to acquire and release the most appropriate lock?

Options

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

How the community answered

(39 responses)
  • A
    3% (1)
  • B
    79% (31)
  • C
    5% (2)
  • D
    13% (5)

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

#ReadWriteLock#ReentrantReadWriteLock#readLock#writeLock

Community Discussion

No community discussion yet for this question.

Full 1Z0-805 Practice