Método de la clase “java.lang.Thread” que bloquea al thread llamante hasta que el thread llamado acabe de ejecutar su método run().
class Repeater |
public class Repeater
extends Thread { private String id; private int count; public Repeater(String id, int count) { this.id = id; this.count = count; } public void run() { do { System.out.println(id + ": " + count); count--; try { Thread.sleep(1000); // 1000ms = 1s } catch (InterruptedException ignore) { } } while (count > 0); System.out.println(id + ": end"); } } |
class WaitingRepeaters |
public class
WaitingRepeaters { public static void main(String[] args) throws InterruptedException { Repeater threadA = new Repeater("A", 2); Repeater threadB = new Repeater("B", 5); threadA.start(); threadB.start(); System.out.println("Repeaters ..."); threadA.join(); System.out.println("A joins"); } } |
> java WaitingRepeaters |
A: 2 Repeaters ... B: 5 A: 1 B: 4 A: end B: 3 A joins B: 2 B: 1 B: end |
Temas relacionados