// The most basic use of the Thread class.
public class BasicThreadsHere { public static void main(String[] args) { Thread p = new Thread(new LiftOff()); p.start(); System.out.println("Waiting for LiftOff"); } } |
Waiting for LiftOff
#0(9), #0(8), #0(7), #0(6), #0(5), #0(4), #0(3), #0(2), #0(1), #0(Liftoff!), |
// An example of CountDownLatch.
import java.util.concurrent.CountDownLatch; class CDLDemo { public static void main(String args[]) { CountDownLatch cdl = new CountDownLatch(5); System.out.println("Starting"); new MyThread(cdl); try { cdl.await(); } catch (InterruptedException exc) { System.out.println(exc); } System.out.println("Done"); } } class MyThread implements Runnable { CountDownLatch latch; MyThread(CountDownLatch c) { latch = c; new Thread(this).start(); } public void run() { for(int i = 0; i<5; i++) { System.out.println(i); latch.countDown(); // decrement count } } } |
Starting
0 1 2 3 4 Done |
import java.util.concurrent.*;
import java.util.*; class TaskWithTheResult implements Callable<String> { private int id; public TaskWithTheResult(int id) { this.id = id; } public String call() { return "result of TaskWithTheResult " + id; } } public class CallableDemo { public static void main(String[] args) { ExecutorService exec = Executors.newCachedThreadPool(); ArrayList<Future<String>> results = new ArrayList<Future<String>>(); for(int i = 0; i < 10; i++) results.add(exec.submit(new TaskWithTheResult(i))); for(Future<String> fs : results) try { // get() blocks until completion: System.out.println(fs.get()); } catch(InterruptedException e) { System.out.println(e); return; } catch(ExecutionException e) { System.out.println(e); } finally { exec.shutdown(); } } } |
result of TaskWithResult 0
result of TaskWithResult 1 result of TaskWithResult 2 result of TaskWithResult 3 result of TaskWithResult 4 result of TaskWithResult 5 result of TaskWithResult 6 result of TaskWithResult 7 result of TaskWithResult 8 result of TaskWithResult 9 |