// Collection of code snippets by Arne Vajhøj 
// (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) 
public class T19 {
    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 testV(int nthreads) throws InterruptedException {
        long t1 = System.currentTimeMillis();
        Thread[] t = new Thread[nthreads];
        for(int i = 0; i < t.length; i++) {
            t[i] = Thread.startVirtualThread(() -> { for(int j = 0; j < NTASKS / nthreads; j++) something(); });
        }
        for(int i = 0; i < t.length; i++) {
            t[i].join();
        }
        long t2 = System.currentTimeMillis();
        System.out.printf("Virtual threads (%d) : %d\n", nthreads, t2 - t1);
    }
    public static void main(String[] args) throws InterruptedException {
        for(int i = 2; i < 16; i++) {
            testV(1 << i);
        }
    }
}
