We would like to know how to start one thread, then start another one and first thread keeps running when thread 2 running.
import java.util.concurrent.CountDownLatch; //w w w.ja v a2s.co m public class Main { static CountDownLatch cdl; public static void main(String... s) { cdl = new CountDownLatch(1); Thread a = new Thread(() -> { System.out.println("started a"); try { Thread.sleep(4000); } catch (InterruptedException e) { e.printStackTrace(); } cdl.countDown(); System.out.println("stoped a"); }); Thread b = new Thread(() -> { System.out.println("started b"); System.out.println("wait a"); try { cdl.await(); } catch (Exception e) { e.printStackTrace(); } System.out.println("stoped b"); }); b.start(); a.start(); } }
The code above generates the following result.