public class ImmutableFlag<T> extends Flag<T> implements Serializable
Flag
whhich value cannot be set.Flag.DoInSync<T>
Constructor and Description |
---|
ImmutableFlag(IFlag<T> flag)
Creates a new instance of ImmutableFlag
|
Modifier and Type | Method and Description |
---|---|
void |
addListener(FlagListener<T> listener)
Adds new listener to the flag with specified param.
|
void |
addStrongListener(FlagListener<T> listener)
Adds new listener to the flag (strong reference).
|
void |
clearListeners()
Call to clear (remove) all the listeners on the flag.
|
void |
defreeze()
Method is synchronized.
|
void |
freeze()
This method will freeze the processing of the setFlag() method.
|
T |
getFlag()
Returns the value of the flag.
|
ImmutableFlag<T> |
getImmutable() |
void |
inSync(Flag.DoInSync<T> command)
Add a command (to the end of the queue) that will be executed in-sync with other changes (you may be sure that no other changes
are taking place right now).
|
boolean |
isFrozen()
Whether the flag-change has been frozen, i.e., setFlag() won't change the flag value
immediately by will wait till
Flag.defreeze() is called. |
boolean |
isListenning(FlagListener<T> listener)
Checks whether listener is already registered (using equals()).
|
void |
removeListener(FlagListener<T> listener)
Removes all registered 'listener' from the flag.
|
void |
setFlag(T newValue)
Changes the flag and informs all listeners.
|
inSyncInner, isNone, isOne, removeAllListeners, waitFor, waitFor, waitForChange, waitForChange
public void addListener(FlagListener<T> listener)
Flag
Note that all anonymous listeners are not subject to gc() because they are reachable from within the object where they were created.
addListener
in interface IFlag<T>
addListener
in class Flag<T>
public void addStrongListener(FlagListener<T> listener)
Flag
addStrongListener
in interface IFlag<T>
addStrongListener
in class Flag<T>
public void clearListeners()
Flag
clearListeners
in interface IFlag<T>
clearListeners
in class Flag<T>
public T getFlag()
Flag
Note that if the flag contains any set-flag pending requests queue it will return the last value from this queue.
This has a big advantage for the multi-thread heavy-listener oriented designs.
Every time a listener is informed about the flag change it receives a new value of the flag but additionally it may query the flag for the last value there will be set into it.
Note that if you use the Flag sparingly this mechanism won't affect you in 99.99999% of time.
Warning - this method won't return truly a correct value if you will use inSync() method because this time we won't be able to obtain the concrete value of the flag after the DoInSync command will be carried out - instead we return the first value we are aware of. Again this won't affect you in any way (... but you should know such behavior exist ;-))
public boolean isListenning(FlagListener<T> listener)
Flag
isListenning
in interface IFlag<T>
isListenning
in class Flag<T>
public void removeListener(FlagListener<T> listener)
Flag
removeListener
in interface IFlag<T>
removeListener
in class Flag<T>
public void setFlag(T newValue)
Flag
public ImmutableFlag<T> getImmutable()
getImmutable
in interface IFlag<T>
getImmutable
in class Flag<T>
public void inSync(Flag.DoInSync<T> command)
Flag
This is also used by the setFlag() method.
public boolean isFrozen()
Flag
Flag.defreeze()
is called.public void freeze()
Flag
It waits until all setFlag() pending requests are resolved and then returns.
It may be used to for the synchronized registration of the listeners (if you really care for the value of the flag before creating the listener). Or it may be used to obtain a true value of the flag in the moment of the method call (as it waits for all the listeners to execute).
In one of these cases, do this:
Example: you have a flag that is counting how many alive threads you have, those threads may be created concurrently ... without this synchronizing method you wouldn't be able to correctly read the number of threads before incrementing the flag.
Beware of deadlocks when using this method, watch out:
a) infinite recursion during the setFlag() (listeners are changing the flag value repeatedly)
b) incorrect sequences of the freeze() / defreeze() calls
Of course you may simulate this behavior with simple synchronized() statement, but this isn't always feasible as it blocks all other threads while accessing this flag, note that this flag implementation promotes non-blocking methods.
Copyright © 2012 AMIS research group, Faculty of Mathematics and Physics, Charles University in Prague, Czech Republic. All Rights Reserved.