mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-12 18:59:06 +01:00
Use CountDownLatch rather than wait/notify.
This commit is contained in:
@@ -3,6 +3,7 @@ package net.sf.briar.plugins.bluetooth;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Enumeration;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import javax.bluetooth.DataElement;
|
||||
@@ -14,29 +15,18 @@ abstract class AbstractListener implements DiscoveryListener {
|
||||
|
||||
protected final DiscoveryAgent discoveryAgent;
|
||||
protected final AtomicInteger searches = new AtomicInteger(1);
|
||||
|
||||
protected boolean finished = false; // Locking: this
|
||||
protected final CountDownLatch finished = new CountDownLatch(1);
|
||||
|
||||
protected AbstractListener(DiscoveryAgent discoveryAgent) {
|
||||
this.discoveryAgent = discoveryAgent;
|
||||
}
|
||||
|
||||
public void inquiryCompleted(int discoveryType) {
|
||||
if(searches.decrementAndGet() == 0) {
|
||||
synchronized(this) {
|
||||
finished = true;
|
||||
notifyAll();
|
||||
}
|
||||
}
|
||||
if(searches.decrementAndGet() == 0) finished.countDown();
|
||||
}
|
||||
|
||||
public void serviceSearchCompleted(int transaction, int response) {
|
||||
if(searches.decrementAndGet() == 0) {
|
||||
synchronized(this) {
|
||||
finished = true;
|
||||
notifyAll();
|
||||
}
|
||||
}
|
||||
if(searches.decrementAndGet() == 0) finished.countDown();
|
||||
}
|
||||
|
||||
protected Object getDataElementValue(Object o) {
|
||||
|
||||
@@ -34,13 +34,11 @@ class ContactListener extends AbstractListener {
|
||||
urls = Collections.synchronizedMap(new HashMap<ContactId, String>());
|
||||
}
|
||||
|
||||
public synchronized Map<ContactId, String> waitForUrls() {
|
||||
while(!finished) {
|
||||
try {
|
||||
wait();
|
||||
} catch(InterruptedException e) {
|
||||
if(LOG.isLoggable(Level.WARNING)) LOG.warning(e.getMessage());
|
||||
}
|
||||
Map<ContactId, String> waitForUrls() {
|
||||
try {
|
||||
finished.await();
|
||||
} catch(InterruptedException e) {
|
||||
if(LOG.isLoggable(Level.WARNING)) LOG.warning(e.getMessage());
|
||||
}
|
||||
return urls;
|
||||
}
|
||||
|
||||
@@ -19,20 +19,18 @@ class InvitationListener extends AbstractListener {
|
||||
|
||||
private final String uuid;
|
||||
|
||||
private String url = null; // Locking: this
|
||||
private volatile String url = null;
|
||||
|
||||
InvitationListener(DiscoveryAgent discoveryAgent, String uuid) {
|
||||
super(discoveryAgent);
|
||||
this.uuid = uuid;
|
||||
}
|
||||
|
||||
synchronized String waitForUrl() {
|
||||
while(!finished) {
|
||||
try {
|
||||
wait();
|
||||
} catch(InterruptedException e) {
|
||||
if(LOG.isLoggable(Level.WARNING)) LOG.warning(e.getMessage());
|
||||
}
|
||||
String waitForUrl() {
|
||||
try {
|
||||
finished.await();
|
||||
} catch(InterruptedException e) {
|
||||
if(LOG.isLoggable(Level.WARNING)) LOG.warning(e.getMessage());
|
||||
}
|
||||
return url;
|
||||
}
|
||||
@@ -60,12 +58,9 @@ class InvitationListener extends AbstractListener {
|
||||
for(String u : uuids) {
|
||||
if(uuid.equalsIgnoreCase(u)) {
|
||||
// The UUID matches - store the URL
|
||||
synchronized(this) {
|
||||
url = serviceUrl;
|
||||
finished = true;
|
||||
notifyAll();
|
||||
return;
|
||||
}
|
||||
url = serviceUrl;
|
||||
finished.countDown();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package net.sf.briar.plugins.file;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
@@ -11,31 +13,29 @@ class FileListener {
|
||||
|
||||
private final String filename;
|
||||
private final long end;
|
||||
private final CountDownLatch finished = new CountDownLatch(1);
|
||||
|
||||
private File file = null; // Locking: this
|
||||
private volatile File file = null;
|
||||
|
||||
FileListener(String filename, long timeout) {
|
||||
this.filename = filename;
|
||||
end = System.currentTimeMillis() + timeout;
|
||||
}
|
||||
|
||||
synchronized File waitForFile() {
|
||||
File waitForFile() {
|
||||
long now = System.currentTimeMillis();
|
||||
while(file == null && now < end) {
|
||||
try {
|
||||
wait(end - now);
|
||||
} catch(InterruptedException e) {
|
||||
if(LOG.isLoggable(Level.WARNING)) LOG.warning(e.getMessage());
|
||||
}
|
||||
now = System.currentTimeMillis();
|
||||
try {
|
||||
finished.await(end - now, TimeUnit.MILLISECONDS);
|
||||
} catch(InterruptedException e) {
|
||||
if(LOG.isLoggable(Level.WARNING)) LOG.warning(e.getMessage());
|
||||
}
|
||||
return file;
|
||||
}
|
||||
|
||||
synchronized void addFile(File f) {
|
||||
void addFile(File f) {
|
||||
if(filename.equals(f.getName())) {
|
||||
file = f;
|
||||
notifyAll();
|
||||
finished.countDown();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user