import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.atomic.AtomicLong; public class Main { private static final int K_SIZE = 1024; private static final long MAX = 300_000_000L; public static void main(String[] args) { int threadCount = Integer.getInteger("thread_count", 4); final int size = Integer.getInteger("size", 5); final long max = Long.getLong("max", MAX); final AtomicLong count = new AtomicLong(); final CountDownLatch beginLatch = new CountDownLatch(1); final CountDownLatch endLatch = new CountDownLatch(threadCount); ExecutorService executor = Executors.newFixedThreadPool(threadCount); for (int i = 0; i < threadCount; i++) { executor.execute(new Runnable() { ConcurrentMap map = new ConcurrentHashMap(); @Override public void run() { try { beginLatch.await(); byte[] data = null; while (!Thread.currentThread().isInterrupted()) { data = new byte[size * K_SIZE]; long current = count.incrementAndGet(); map.put(current, current); data[0] = (byte) current; if (current >= max) { endLatch.countDown(); break; } else if ((current % 1000) == 0) { map.clear(); } } } catch (InterruptedException e) { Thread.currentThread().interrupt(); } } }); } long startTime = System.currentTimeMillis(); beginLatch.countDown(); try { endLatch.await(); long endTime = System.currentTimeMillis(); System.out.println(endTime - startTime); } catch (InterruptedException e) { } executor.shutdown(); } }