rava2
home
documentation
examples
// retoor <retoor@molodetz.nl> public class Main { public static void main(String[] args) { System.out.println("=== Thread Timing Example ===\n"); Thread fast = new Thread(() -> { timedTask("Fast", 50); }); Thread medium = new Thread(() -> { timedTask("Medium", 150); }); Thread slow = new Thread(() -> { timedTask("Slow", 300); }); long startTime = System.currentTimeMillis(); System.out.println("Starting all threads...\n"); fast.start(); medium.start(); slow.start(); fast.join(); long t1 = System.currentTimeMillis() - startTime; System.out.println("Fast joined at +" + t1 + "ms"); medium.join(); long t2 = System.currentTimeMillis() - startTime; System.out.println("Medium joined at +" + t2 + "ms"); slow.join(); long t3 = System.currentTimeMillis() - startTime; System.out.println("Slow joined at +" + t3 + "ms"); long total = System.currentTimeMillis() - startTime; System.out.println("\nAll threads completed in " + total + "ms"); } static void timedTask(String name, int sleepTime) { System.out.println("[" + name + "] Started, sleeping " + sleepTime + "ms"); Thread.sleep(sleepTime); System.out.println("[" + name + "] Done"); } }
run program
ready