sample counter code
package thread;
import java.util.concurrent.atomic.AtomicLong;
public class AtomicCounter extends Thread {
private static AtomicLong counter=new AtomicLong(10);
public static void main(String[] args) throws Exception {
AtomicCounter thread1=new AtomicCounter();
thread1.start();
AtomicCounter thread2=new AtomicCounter();
thread2.start();
AtomicCounter thread3=new AtomicCounter();
thread3.start();
}
@Override
public void run() {
Thread t=Thread.currentThread();
long id=t.getId();
for(long i=0; i<10; i++) {
long nextLong=counter.incrementAndGet();
System.out.println(nextLong +"\t"+id);
Sleep(10);
}
}
public static void Sleep(int ms) {
try {
Thread.sleep(ms);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}