Biblioteca Java - Blame information for rev 28
Subversion Repositories:
Rev | Author | Line No. | Line |
---|---|---|---|
28 | mihai | 1 | package exemple.fire.latch; |
2 | |||
3 | import java.util.concurrent.*; | ||
4 | |||
5 | /* | ||
6 | * The next synchronization control is called a latch. Latching variables specify | ||
7 | * conditions that once set never change. This provides a way to start several | ||
8 | * threads and have them wait until a signal is received from a coordinating thread. | ||
9 | * Latching works well with initialization tasks, where you want no process to run | ||
10 | * until everything needed is initialized. Latching also works well for multiplayer | ||
11 | * games, where you don't want any player to start until all players have joined. | ||
12 | * | ||
13 | * The CountDownLatch class encapsulates a latch that is initialized with a given | ||
14 | * count. The constructor takes the count as its one argument. The count works in | ||
15 | * a way similar to the parties argument to the CyclicBarrier constructor. It | ||
16 | * indicates how many times a countDown method must be called, one for each party | ||
17 | * involved. After the full count is reached, any threads waiting because of an await | ||
18 | * method are released. | ||
19 | * | ||
20 | * The following program, LatchTest, demonstrates the use of a CountDownLatch. The | ||
21 | * program creates a set of threads, but doesn't let any thread start until all the | ||
22 | * threads are created. | ||
23 | * | ||
24 | * The main thread awaits on a second latch to make sure that the other threads are | ||
25 | * finished. This example could have simply used a join operation on the created | ||
26 | * threads, however imagine if you needed to synchronize threads at different phases | ||
27 | * through their lifetime, instead of just at the end. | ||
28 | * | ||
29 | */ | ||
30 | public class LatchTest { | ||
31 | private static final int COUNT = 10; | ||
32 | private static class Worker implements Runnable { | ||
33 | CountDownLatch startLatch; | ||
34 | CountDownLatch stopLatch; | ||
35 | String name; | ||
36 | Worker(CountDownLatch startLatch, | ||
37 | CountDownLatch stopLatch, String name) { | ||
38 | this.startLatch = startLatch; | ||
39 | this.stopLatch = stopLatch; | ||
40 | this.name = name; | ||
41 | } | ||
42 | |||
43 | public void run() { | ||
44 | try { | ||
45 | startLatch.await(); | ||
46 | } catch (InterruptedException ex) { | ||
47 | ex.printStackTrace(); | ||
48 | } | ||
49 | System.out.println("Running: " + name); | ||
50 | stopLatch.countDown(); | ||
51 | } | ||
52 | } | ||
53 | |||
54 | public static void main(String args[]) { | ||
55 | CountDownLatch startSignal = new CountDownLatch(1); | ||
56 | CountDownLatch stopSignal = new CountDownLatch(COUNT); | ||
57 | for (int i=0; i<COUNT; i++) { | ||
58 | new Thread( | ||
59 | new Worker(startSignal, stopSignal, | ||
60 | Integer.toString(i))).start(); | ||
61 | } | ||
62 | System.out.println("Go"); | ||
63 | startSignal.countDown(); | ||
64 | try { | ||
65 | stopSignal.await(); | ||
66 | } catch (InterruptedException ex) { | ||
67 | ex.printStackTrace(); | ||
68 | } | ||
69 | System.out.println("Done"); | ||
70 | } | ||
71 | } |