Ask your thread to report the synchronization object when it is finished, and ask another thread to wait x the number of milliseconds for it to end.
public class Main { private static final Object mThreadLock = new Object(); static class DoTaskThread extends Thread { public void run() { try { int wait = new Random().nextInt(10000); System.out.println("Waiting " + wait + " ms"); Thread.sleep(wait); } catch (InterruptedException ex) { } synchronized (mThreadLock) { mThreadLock.notifyAll(); } } } public static void main(String[] args) { synchronized (mThreadLock) { DoTaskThread thread = new DoTaskThread(); thread.start(); try {
source share