mirror of
https://github.com/bisq-network/bisq.git
synced 2025-02-22 22:45:21 +01:00
Refactored lock implementation
This commit is contained in:
parent
2aaa81b003
commit
202acac74c
2 changed files with 17 additions and 14 deletions
|
@ -29,15 +29,25 @@ public class ThreadGate {
|
|||
private CountDownLatch lock = new CountDownLatch(0);
|
||||
|
||||
/**
|
||||
* Make everyone wait until the gate is opened again.
|
||||
* Make everyone wait until the gate is open again.
|
||||
*/
|
||||
public void engage() {
|
||||
lock = new CountDownLatch(1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Wait for the gate to be opened. Blocks until the gate is opened again.
|
||||
* Returns immediately if the gate is already open.
|
||||
* Make everyone wait until the gate is open again.
|
||||
*
|
||||
* @param numberOfLocks how often the gate has to be unlocked until the gate
|
||||
* opens.
|
||||
*/
|
||||
public void engage(int numberOfLocks) {
|
||||
lock = new CountDownLatch(numberOfLocks);
|
||||
}
|
||||
|
||||
/**
|
||||
* Wait for the gate to be opened. Blocks until the gate is open again. Returns
|
||||
* immediately if the gate is already open.
|
||||
*/
|
||||
public synchronized void await() {
|
||||
while (lock.getCount() > 0)
|
||||
|
|
|
@ -24,11 +24,9 @@ import java.util.HashMap;
|
|||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Properties;
|
||||
import java.util.Random;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
@ -77,10 +75,10 @@ public class P2PNetworkLoad extends Metric implements MessageListener, SetupList
|
|||
private final File torHiddenServiceDir = new File("metric_p2pNetworkLoad");
|
||||
private int nonce;
|
||||
private Map<NodeAddress, Map<String, Counter>> bucketsPerHost = new ConcurrentHashMap<>();
|
||||
private CountDownLatch latch;
|
||||
private Set<byte[]> hashes = new HashSet<>();
|
||||
private boolean reportFindings;
|
||||
private final ThreadGate hsReady = new ThreadGate();
|
||||
private final ThreadGate gate = new ThreadGate();
|
||||
|
||||
/**
|
||||
* Efficient way to count message occurrences.
|
||||
|
@ -170,18 +168,13 @@ public class P2PNetworkLoad extends Metric implements MessageListener, SetupList
|
|||
}, current));
|
||||
}
|
||||
|
||||
latch = new CountDownLatch(threadList.size());
|
||||
gate.engage(threadList.size());
|
||||
|
||||
// start all threads and wait until they all finished. We do that so we can
|
||||
// minimize the time between querying the hosts and therefore the chance of
|
||||
// inconsistencies.
|
||||
threadList.forEach(Thread::start);
|
||||
try {
|
||||
latch.await();
|
||||
} catch (InterruptedException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
gate.await();
|
||||
|
||||
// report
|
||||
Map<String, String> report = new HashMap<>();
|
||||
|
@ -266,7 +259,7 @@ public class P2PNetworkLoad extends Metric implements MessageListener, SetupList
|
|||
bucketsPerHost.put(connection.peersNodeAddressProperty().getValue(), buckets);
|
||||
|
||||
connection.removeMessageListener(this);
|
||||
latch.countDown();
|
||||
gate.proceed();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue