mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-14 11:49:04 +01:00
Use CopyOnWriteArrayList for listener lists.
This commit is contained in:
@@ -13,8 +13,8 @@ import java.util.Collection;
|
|||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.CopyOnWriteArrayList;
|
||||||
import java.util.concurrent.locks.ReentrantReadWriteLock;
|
import java.util.concurrent.locks.ReentrantReadWriteLock;
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
@@ -105,8 +105,8 @@ DatabaseCleaner.Callback {
|
|||||||
private final DatabaseCleaner cleaner;
|
private final DatabaseCleaner cleaner;
|
||||||
private final ShutdownManager shutdown;
|
private final ShutdownManager shutdown;
|
||||||
|
|
||||||
private final List<DatabaseListener> listeners =
|
private final Collection<DatabaseListener> listeners =
|
||||||
new ArrayList<DatabaseListener>(); // Locking: self
|
new CopyOnWriteArrayList<DatabaseListener>();
|
||||||
|
|
||||||
private final Object spaceLock = new Object();
|
private final Object spaceLock = new Object();
|
||||||
private long bytesStoredSinceLastCheck = 0L; // Locking: spaceLock
|
private long bytesStoredSinceLastCheck = 0L; // Locking: spaceLock
|
||||||
@@ -161,15 +161,11 @@ DatabaseCleaner.Callback {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void addListener(DatabaseListener d) {
|
public void addListener(DatabaseListener d) {
|
||||||
synchronized(listeners) {
|
listeners.add(d);
|
||||||
listeners.add(d);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void removeListener(DatabaseListener d) {
|
public void removeListener(DatabaseListener d) {
|
||||||
synchronized(listeners) {
|
listeners.remove(d);
|
||||||
listeners.remove(d);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public ContactId addContact(byte[] inSecret, byte[] outSecret)
|
public ContactId addContact(byte[] inSecret, byte[] outSecret)
|
||||||
@@ -198,15 +194,7 @@ DatabaseCleaner.Callback {
|
|||||||
|
|
||||||
/** Notifies all listeners of a database event. */
|
/** Notifies all listeners of a database event. */
|
||||||
private void callListeners(DatabaseEvent e) {
|
private void callListeners(DatabaseEvent e) {
|
||||||
List<DatabaseListener> copy;
|
for(DatabaseListener d : listeners) d.eventOccurred(e);
|
||||||
synchronized(listeners) {
|
|
||||||
if(listeners.isEmpty()) return;
|
|
||||||
copy = new ArrayList<DatabaseListener>(listeners);
|
|
||||||
}
|
|
||||||
// Shuffle the listeners so we don't always send new messages
|
|
||||||
// to contacts in the same order
|
|
||||||
Collections.shuffle(copy);
|
|
||||||
for(DatabaseListener d : copy) d.eventOccurred(e);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addLocalGroupMessage(Message m) throws DbException {
|
public void addLocalGroupMessage(Message m) throws DbException {
|
||||||
|
|||||||
@@ -6,12 +6,12 @@ import java.io.File;
|
|||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.PrintStream;
|
import java.io.PrintStream;
|
||||||
import java.util.HashSet;
|
import java.util.Collection;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
import java.util.MissingResourceException;
|
import java.util.MissingResourceException;
|
||||||
import java.util.ResourceBundle;
|
import java.util.ResourceBundle;
|
||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
import java.util.Set;
|
import java.util.concurrent.CopyOnWriteArrayList;
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
@@ -77,7 +77,8 @@ public class I18nImpl implements I18n {
|
|||||||
|
|
||||||
private final Object bundleLock = new Object();
|
private final Object bundleLock = new Object();
|
||||||
private final ClassLoader loader = I18n.class.getClassLoader();
|
private final ClassLoader loader = I18n.class.getClassLoader();
|
||||||
private final Set<Listener> listeners = new HashSet<Listener>();
|
private final Collection<Listener> listeners =
|
||||||
|
new CopyOnWriteArrayList<Listener>();
|
||||||
private final FontManager fontManager;
|
private final FontManager fontManager;
|
||||||
|
|
||||||
private volatile Locale locale = Locale.getDefault();
|
private volatile Locale locale = Locale.getDefault();
|
||||||
@@ -123,9 +124,7 @@ public class I18nImpl implements I18n {
|
|||||||
this.locale = locale;
|
this.locale = locale;
|
||||||
Locale.setDefault(locale);
|
Locale.setDefault(locale);
|
||||||
bundle = null;
|
bundle = null;
|
||||||
synchronized(listeners) {
|
for(Listener l : listeners) l.localeChanged(uiFont);
|
||||||
for(Listener l : listeners) l.localeChanged(uiFont);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -157,14 +156,10 @@ public class I18nImpl implements I18n {
|
|||||||
|
|
||||||
public void addListener(Listener l) {
|
public void addListener(Listener l) {
|
||||||
l.localeChanged(fontManager.getUiFont());
|
l.localeChanged(fontManager.getUiFont());
|
||||||
synchronized(listeners) {
|
listeners.add(l);
|
||||||
listeners.add(l);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void removeListener(Listener l) {
|
public void removeListener(Listener l) {
|
||||||
synchronized(listeners) {
|
listeners.remove(l);
|
||||||
listeners.remove(l);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user