Skip to content

parallel counting in Java using AtomicInteger

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.TimeUnit;

class ParallelCounterExample {
public static void main(String[] args) throws InterruptedException {
int threads = 2000;
ExecutorService executor = Executors.newFixedThreadPool(threads);
final AtomicInteger atomi = new AtomicInteger();
for(int i=0; i<threads; i++) {
executor.execute( new Runnable() {
public void run() {
executor.awaitTermination(1, TimeUnit.SECONDS);

Published inenglish

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *