public class Mptest { static int runCount_now = 0; static int tasksLeft = 0; public static void main(String[] args) { int cpu = Runtime.getRuntime().availableProcessors(); System.out.println("Cpu count: " + cpu); tasksLeft = 3*cpu; System.out.println("Task count: " + tasksLeft); for (int i = 1; i <= tasksLeft; i++) { cpuTest(i); tasksLeft = 3*cpu; } } static public void cpuTest(int threadCount) { long start = System.currentTimeMillis(); final Object object = new Object(); for (int i = 0; i < threadCount; i++) { Runnable runnable = new Runnable() { public void run() { boolean go_on = true; while(go_on) { spendTime(); synchronized (object) { if(tasksLeft > 0) tasksLeft--; else { go_on = false; runCount_now--; } object.notify(); } } } }; Thread thread = new Thread(runnable); synchronized (object) { runCount_now++; } thread.start(); } try { synchronized (object) { while(runCount_now > 0 || tasksLeft > 0) { //System.out.println("runCount_now: " + runCount_now); //System.out.println("tasksLeft: " + tasksLeft); object.wait(); } } } catch (InterruptedException e) { e.printStackTrace(); } long end = System.currentTimeMillis(); System.out.println("Time with " + threadCount + " threads : " + (end - start)); } static public int spendTime() { int runCount = 20000; int sum = 0; for (int i = 0; i < runCount; i++) for (int j = 0; j < runCount; j++) sum += j - i; //System.out.println("sum: " + sum); return sum; } }