Q:

Acquire WriteLock before writing shared resource

I am a beginner in multi-threading and came across this example on ReadWriteLock.
ScoreBoard 
public class ScoreBoard {
private boolean scoreUpdated = false;
private int score = 0;
    String health = "Not Available";
final ReentrantReadWriteLock rrwl = new ReentrantReadWriteLock();
public String getMatchHealth() {
    rrwl.readLock().lock();
    if (scoreUpdated) {
        rrwl.readLock().unlock();
        rrwl.writeLock().lock();
        try {
          if (scoreUpdated) {
              score = fetchScore();
              scoreUpdated = false;
          }
          rrwl.readLock().lock();
        } finally {
          rrwl.writeLock().unlock(); 
        }   
    }   
    try {
         if (score % 2 == 0 ){
              health = "Bad Score";
          } else {
              health = "Good Score";
          }
    } finally {
          rrwl.readLock().unlock();
        }
    return health;
}
public void updateScore() {
    try {
        rrwl.writeLock().lock();
        //perform more task here
        scoreUpdated = true;
    }finally {
        rrwl.writeLock().unlock();
    }
}
private int fetchScore() {
    Calendar calender = Calendar.getInstance();
    return calender.get(Calendar.MILLISECOND);
}
}

ScoreUpdateThread
public class ScoreUpdateThread implements Runnable {
private ScoreBoard scoreBoard;
public ScoreUpdateThread(ScoreBoard scoreTable) {
    this.scoreBoard = scoreTable;
}
@Override
public void run() {
    for(int i= 0; i < 5; i++) {
        System.out.println("Score Updated.");       
        scoreBoard.updateScore();
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
}

Main 
public class Main {
public static void main(String[] args) {
    final int threadCount = 2;
    final ExecutorService exService = Executors.newFixedThreadPool(threadCount);
    final ScoreBoard scoreBoard = new ScoreBoard();
    exService.execute(new ScoreUpdateThread(scoreBoard));
    exService.execute(new ScoreHealthThread(scoreBoard));           
    exService.shutdown();
}
}   

Wont in the ScoreBoard while updating the health variable , we need to acquire the WriteLock since we are updating a shared variable ?

A:

Wont in the ScoreBoard while updating the health variable , we need to acquire the WriteLock since we are updating a shared variable ?

You are correct that the class's getMatchHealth() method performs a modification of the shared health variable without holding the write lock.  There being no other mechanism in the class for synchronizinging those writes, this produces a data race when two threads invoke getMatchHealth() on the same ScoreBoard without engaging some form of external synchronization.  This appears to be a flaw in the method, and the method appears to have some other, more subtle synchronization issues, too.
Nevertheless, the program presented appears not ever to invoke getMatchHealth() at all, and the other ScoreBoard methods appear to be free of synchronization flaws, so the particular program presented is not affected by the flaws.  Still, unless those flaws are intentional -- for didactic purposes, for instance -- I would recommend finding a better source of tutorial material than the one that provided the example program.

