Following is the code - AtomicInteger
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;
public class ExecutorExample1 {
public static void main(String[] args) {
ExecutorService executorService= Executors.newFixedThreadPool(2);
executorService.execute(new MyTask());
executorService.execute(new MyTask());
executorService.execute(new MyTask());
executorService.shutdown();
}
}
class MyTask implements Runnable{
private static AtomicInteger count = new AtomicInteger(0);
@Override
public void run() {
try {
count.addAndGet(1);
task();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private void task()throws InterruptedException{
System.out.println(count + " Enterd Run of: " + Thread.currentThread().getName());
Thread.sleep(5000);
System.out.println(count + " Executing: " + Thread.currentThread().getName());
Thread.sleep(5000);
System.out.println(count + " Completed Executing: " + Thread.currentThread().getName());
}
}
Ouput of above code:
2 Enterd Run of: pool-1-thread-1
2 Enterd Run of: pool-1-thread-2
2 Executing: pool-1-thread-2
2 Executing: pool-1-thread-1
2 Completed Executing: pool-1-thread-1
2 Completed Executing: pool-1-thread-2
3 Enterd Run of: pool-1-thread-1
3 Executing: pool-1-thread-1
3 Completed Executing: pool-1-thread-1
Same code replacing AtomicInteger with int and synchronized block
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;
public class ExecutorExample1 {
public static void main(String[] args) {
ExecutorService executorService= Executors.newFixedThreadPool(2);
executorService.execute(new MyTask());
executorService.execute(new MyTask());
executorService.execute(new MyTask());
executorService.shutdown();
}
}
class MyTask implements Runnable{
//private static AtomicInteger count = new AtomicInteger(0);
private static int count = 0;
@Override
public void run() {
try {
//count.addAndGet(1);
synchronized (MyTask.class){
count+=1;
}
task();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private void task()throws InterruptedException{
System.out.println(count + " Enterd Run of: " + Thread.currentThread().getName());
Thread.sleep(5000);
System.out.println(count + " Executing: " + Thread.currentThread().getName());
Thread.sleep(5000);
System.out.println(count + " Completed Executing: " + Thread.currentThread().getName());
}
}
Output of Synchronized block code
2 Enterd Run of: pool-1-thread-2
1 Enterd Run of: pool-1-thread-1
2 Executing: pool-1-thread-2
2 Executing: pool-1-thread-1
2 Completed Executing: pool-1-thread-2
2 Completed Executing: pool-1-thread-1
3 Enterd Run of: pool-1-thread-2
3 Executing: pool-1-thread-2
3 Completed Executing: pool-1-thread-2
Question?
- Why is there a difference in outputs?
- Why the atomic integer is getting incremented to 2 instead of 1.
- How do I acheive the synchronised output with atomicinteger.
- Any benefit or use of using volatile and atomic together?