With following class,
// This class should be thread-safe!!! class BankAccount { private long balance; // Should it
No, compared with synchronized keyword, volatile is lightweight.
synchronized
volatile
volatile can gurantee the reader thread always get fresh balance value, but it can not make balance += amount; atomic. synchronized can do both.
balance
balance += amount;