About Question enthuware.ocpjp.v7.2.1083 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
kashyapa
Posts: 23
Joined: Thu May 08, 2014 5:27 am
Contact:

About Question enthuware.ocpjp.v7.2.1083 :

Post by kashyapa »

Code: Select all

ai.set(ai.get()+1);
why this is wrong???? :(

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1083 :

Post by admin »

The whole purpose of using AtomicInteger is to increment atomically i.e. in a single operation. In this option, you are incrementing ai in two separate calls, which is not thread safe. Between the time when ai.get() returns a value and ai.set() is called, some other thread may call ai.set with the same value. This will make the count incorrect.
HTH,
Paul.

Post Reply

Who is online

Users browsing this forum: No registered users and 4 guests