Optimistic and Pessimistic Locking in Java