import chilldev.commons.concurrent.FutureTaskEx; public class FutureTaskExample { public static void main(String[] args) { FutureTaskEx<String> futureTask = new FutureTaskEx<>(() -> { return "Task complete"; }); Thread thread = new Thread(futureTask); thread.start(); try { String result = futureTask.get(); System.out.println(result); } catch (InterruptedException e) { e.printStackTrace(); } } } import chilldev.commons.concurrent.CountDownLatchEx; public class CountDownLatchExample { public static void main(String[] args) { CountDownLatchEx latch = new CountDownLatchEx(2); Runnable task = () -> { try { // ... } finally { latch.countDown(); } }; Thread thread1 = new Thread(task); Thread thread2 = new Thread(task); thread1.start(); thread2.start(); try { latch.await(); System.out.println("All tasks completed"); } catch (InterruptedException e) { e.printStackTrace(); } } }


上一篇:
下一篇:
切换中文