// Collection of code snippets by Arne Vajhøj 
// (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) 
public class T18 {
    public static void something() {
        try {
            Thread.sleep(1); // simulate work taking time
        } catch(InterruptedException ex) {
            ex.printStackTrace();
        }
    }
    private static final int NTASKS = 1 << 16;
    public static void testR(int nthreads) throws InterruptedException {
        long t1 = System.currentTimeMillis();
        Thread[] t = new Thread[nthreads];
        for(int i = 0; i < t.length; i++) {
            t[i] = new Thread(() -> { for(int j = 0; j < NTASKS / nthreads; j++) something(); });
            t[i].start();
        }
        for(int i = 0; i < t.length; i++) {
            t[i].join();
        }
        long t2 = System.currentTimeMillis();
        System.out.printf("Real threads (%d) : %d\n", nthreads, t2 - t1);
    }
    public static void main(String[] args) throws InterruptedException {
        for(int i = 2; i < 16; i++) {
            testR(1 << i);
        }
    }
}
