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