AtomicInteger for limited sequnce generation
Posted
by satish
on Stack Overflow
See other posts from Stack Overflow
or by satish
Published on 2010-06-02T17:25:50Z
Indexed on
2010/06/02
17:33 UTC
Read the original article
Hit count: 150
How can we use AtomicInteger for limited sequence generation say the sequence number has to be between 1 to 60. Once the sequece reaches 60 it has to start again from 1. I wrote this code though not quite sure wether this is thread safe or not?
public int getNextValue()
{
int v;
do
{
v = val.get();
if ( v == 60)
{
val.set(1);
}
}
while (!val.compareAndSet(v , v + 1));
return v + 1;
}
© Stack Overflow or respective owner