90. Atomic Integer - prabhatrocks07/Core-Java-Programming GitHub Wiki

public class AtomicIntConcurreny {

public static void main(String[] args) throws InterruptedException {

	ProcessingThreadAtomic pt = new ProcessingThreadAtomic();
    Thread t1 = new Thread(pt, "t1");
    t1.start();
    Thread t2 = new Thread(pt, "t2");
    t2.start();
    t1.join();
    t2.join();
    System.out.println("Processing count=" + pt.getCount());
}
}

class ProcessingThreadAtomic implements Runnable {
private AtomicInteger count = new AtomicInteger();
//private int count;
@Override
public void run() {
    for (int i = 1; i < 5; i++) {
        processSomething(i);
        //count++;
        count.incrementAndGet();
    }
}

public int getCount() {
	//return this.count;
    return this.count.get();
}

private void processSomething(int i) {
    // processing some job
    try {
        Thread.sleep(i * 1000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}
}