I have defined an atomicinterger variable in class. The class also extends thread class .I have created two threads and incrementing the value of the atomic integer in run method .I was excepting the value of 2 after running two threads, But I am getting value as 1. Please let me know what went wrong in the below code.
public class AtomicIntergerTest extends Thread {
AtomicInteger i = new AtomicInteger();
int j = 0;
@Override
public void run() {
i.incrementAndGet();
}
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
AtomicIntergerTest th1 = new AtomicIntergerTest();
AtomicIntergerTest th2 = new AtomicIntergerTest();
th1.start();
th2.start();
th1.join();
th2.join();
System.out.println(th2.i.get());
}
}