I'm new to threads. How can I get t.join
to work, whereby the thread calling it waits until t is done executing?
This code would just freeze the program, because the thread is waiting for itself to die, right?
public static void main(String[] args) throws InterruptedException {
Thread t0 = new Thready();
t0.start();
}
@Override
public void run() {
for (String s : info) {
try {
join();
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.printf("%s %s%n", getName(), s);
}
}
What would I do if I wanted to have two threads, one of which prints out half the info
array, then waits for the other to finish before doing the rest?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…