import java.util.concurrent.atomic.AtomicLong;
/**
-- A long value that may be updated atomically. You can refer the java.util.concurrent.atomic package properties of atomic variables.
-- An AtomicLong is used in applications such as atomically incremented sequence numbers, and cannot be used as a replacement for a java.lang.Long.
-- However, this class does extend Number to allow uniform access by tools and utilities that deal with numerically-based classes.
*/
public class TestAtomicLong
{
private static TestAtomicLong instance;
private final AtomicLong sequenceNumber ;
static {
instance = new TestAtomicLong() ;
}
private TestAtomicLong(){
sequenceNumber = new AtomicLong(1) ;
}
public void increatementSequence(){
System.out.println(sequenceNumber.incrementAndGet() );
System.out.println(sequenceNumber.get()) ;
}
private class ScoreCounter implements Runnable{
public ScoreCounter() {
}
/* (non-Javadoc)
* @see java.lang.Runnable#run()
*/
@Override
public void run()
{
sequenceNumber.incrementAndGet() ;
System.out.println("Score Manager :" + Thread.currentThread().getName() + " updated the score : " + sequenceNumber.get()) ;
}
}
private void runScoreManager() {
for(int i=0; i<10; i++) {
ScoreCounter sc = new ScoreCounter() ;
Thread scoreBoardManager = new Thread(sc) ;
scoreBoardManager.start() ;
}
}
public static void main(String str[]) throws Exception {
TestAtomicLong tal = new TestAtomicLong() ;
tal.runScoreManager() ;
}
}
No comments:
Post a Comment