I'm trying to use AtomicLong in Multi Threading Environment. My desired result is not working,
public class Account implements Runnable {
private final AtomicLong amount = new AtomicLong(0);
public Account(long difference) {
amount.set(difference);
}
public void run() {
System.out.println("The Balance is : " + amount);
}
}
public class Examples {
public static void main(String[] args) {
ExecutorService service = Executors.newFixedThreadPool(10);
IntStream.range(0, 100).forEach(i -> service.submit(new Account(i)));
}
}
I tried to use Read/Write Lock and block level synchronization but no luck. Could please someone spot where am doing mistake?
I output has to be starts from 0,1,2...99.