annotate src/main/java/suikwasha/distributedalgorithm/simulator/Synchronizer.java @ 0:38a110b13db1

added SimpleDistributedAlgorithmFramework. added NaiveAlgorithm added ChangRobertsAlgorithm added PertersonAlgorithm
author suikwasha
date Fri, 19 Oct 2012 00:05:41 +0900
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
38a110b13db1 added SimpleDistributedAlgorithmFramework.
suikwasha
parents:
diff changeset
1 package suikwasha.distributedalgorithm.simulator;
38a110b13db1 added SimpleDistributedAlgorithmFramework.
suikwasha
parents:
diff changeset
2
38a110b13db1 added SimpleDistributedAlgorithmFramework.
suikwasha
parents:
diff changeset
3 import java.util.concurrent.CountDownLatch;
38a110b13db1 added SimpleDistributedAlgorithmFramework.
suikwasha
parents:
diff changeset
4 import java.util.concurrent.atomic.AtomicLong;
38a110b13db1 added SimpleDistributedAlgorithmFramework.
suikwasha
parents:
diff changeset
5
38a110b13db1 added SimpleDistributedAlgorithmFramework.
suikwasha
parents:
diff changeset
6 public class Synchronizer
38a110b13db1 added SimpleDistributedAlgorithmFramework.
suikwasha
parents:
diff changeset
7 {
38a110b13db1 added SimpleDistributedAlgorithmFramework.
suikwasha
parents:
diff changeset
8 private AtomicLong counter;
38a110b13db1 added SimpleDistributedAlgorithmFramework.
suikwasha
parents:
diff changeset
9 private CountDownLatch latch;
38a110b13db1 added SimpleDistributedAlgorithmFramework.
suikwasha
parents:
diff changeset
10
38a110b13db1 added SimpleDistributedAlgorithmFramework.
suikwasha
parents:
diff changeset
11 public Synchronizer()
38a110b13db1 added SimpleDistributedAlgorithmFramework.
suikwasha
parents:
diff changeset
12 {
38a110b13db1 added SimpleDistributedAlgorithmFramework.
suikwasha
parents:
diff changeset
13 counter = new AtomicLong();
38a110b13db1 added SimpleDistributedAlgorithmFramework.
suikwasha
parents:
diff changeset
14 latch = new CountDownLatch(1);
38a110b13db1 added SimpleDistributedAlgorithmFramework.
suikwasha
parents:
diff changeset
15 }
38a110b13db1 added SimpleDistributedAlgorithmFramework.
suikwasha
parents:
diff changeset
16
38a110b13db1 added SimpleDistributedAlgorithmFramework.
suikwasha
parents:
diff changeset
17 public long countup() throws IllegalStateException
38a110b13db1 added SimpleDistributedAlgorithmFramework.
suikwasha
parents:
diff changeset
18 {
38a110b13db1 added SimpleDistributedAlgorithmFramework.
suikwasha
parents:
diff changeset
19 // double checking , is it effective?
38a110b13db1 added SimpleDistributedAlgorithmFramework.
suikwasha
parents:
diff changeset
20 if(latch.getCount() != 0){
38a110b13db1 added SimpleDistributedAlgorithmFramework.
suikwasha
parents:
diff changeset
21 long value = counter.incrementAndGet();
38a110b13db1 added SimpleDistributedAlgorithmFramework.
suikwasha
parents:
diff changeset
22 if(latch.getCount() != 0){
38a110b13db1 added SimpleDistributedAlgorithmFramework.
suikwasha
parents:
diff changeset
23 return value;
38a110b13db1 added SimpleDistributedAlgorithmFramework.
suikwasha
parents:
diff changeset
24 }
38a110b13db1 added SimpleDistributedAlgorithmFramework.
suikwasha
parents:
diff changeset
25 }
38a110b13db1 added SimpleDistributedAlgorithmFramework.
suikwasha
parents:
diff changeset
26
38a110b13db1 added SimpleDistributedAlgorithmFramework.
suikwasha
parents:
diff changeset
27 throw new IllegalStateException("latch.getCount() == 0");
38a110b13db1 added SimpleDistributedAlgorithmFramework.
suikwasha
parents:
diff changeset
28 }
38a110b13db1 added SimpleDistributedAlgorithmFramework.
suikwasha
parents:
diff changeset
29
38a110b13db1 added SimpleDistributedAlgorithmFramework.
suikwasha
parents:
diff changeset
30 public long countdown() throws IllegalStateException
38a110b13db1 added SimpleDistributedAlgorithmFramework.
suikwasha
parents:
diff changeset
31 {
38a110b13db1 added SimpleDistributedAlgorithmFramework.
suikwasha
parents:
diff changeset
32 long value = counter.decrementAndGet();
38a110b13db1 added SimpleDistributedAlgorithmFramework.
suikwasha
parents:
diff changeset
33 if(value < 0){
38a110b13db1 added SimpleDistributedAlgorithmFramework.
suikwasha
parents:
diff changeset
34 throw new IllegalStateException("counter < 0");
38a110b13db1 added SimpleDistributedAlgorithmFramework.
suikwasha
parents:
diff changeset
35 }
38a110b13db1 added SimpleDistributedAlgorithmFramework.
suikwasha
parents:
diff changeset
36
38a110b13db1 added SimpleDistributedAlgorithmFramework.
suikwasha
parents:
diff changeset
37 if(value == 0){
38a110b13db1 added SimpleDistributedAlgorithmFramework.
suikwasha
parents:
diff changeset
38 latch.countDown();
38a110b13db1 added SimpleDistributedAlgorithmFramework.
suikwasha
parents:
diff changeset
39 }
38a110b13db1 added SimpleDistributedAlgorithmFramework.
suikwasha
parents:
diff changeset
40
38a110b13db1 added SimpleDistributedAlgorithmFramework.
suikwasha
parents:
diff changeset
41 return value;
38a110b13db1 added SimpleDistributedAlgorithmFramework.
suikwasha
parents:
diff changeset
42 }
38a110b13db1 added SimpleDistributedAlgorithmFramework.
suikwasha
parents:
diff changeset
43
38a110b13db1 added SimpleDistributedAlgorithmFramework.
suikwasha
parents:
diff changeset
44 public void await() throws InterruptedException
38a110b13db1 added SimpleDistributedAlgorithmFramework.
suikwasha
parents:
diff changeset
45 {
38a110b13db1 added SimpleDistributedAlgorithmFramework.
suikwasha
parents:
diff changeset
46 latch.await();
38a110b13db1 added SimpleDistributedAlgorithmFramework.
suikwasha
parents:
diff changeset
47 }
38a110b13db1 added SimpleDistributedAlgorithmFramework.
suikwasha
parents:
diff changeset
48 }