|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object cz.cuni.amis.utils.flag.Flag<T> cz.cuni.amis.utils.flag.ImmutableFlag<T>
public class ImmutableFlag<T>
Flag
whhich value cannot be set.
Nested Class Summary |
---|
Nested classes/interfaces inherited from class cz.cuni.amis.utils.flag.Flag |
---|
Flag.DoInSync<T> |
Field Summary | |
---|---|
protected IFlag<T> |
flag
|
Constructor Summary | |
---|---|
ImmutableFlag(IFlag<T> flag)
Creates a new instance of ImmutableFlag |
Method Summary | |
---|---|
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. |
Methods inherited from class cz.cuni.amis.utils.flag.Flag |
---|
inSyncInner, isNone, isOne, removeAllListeners, waitFor, waitFor, waitForChange, waitForChange |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
protected IFlag<T> flag
Constructor Detail |
---|
public ImmutableFlag(IFlag<T> flag)
Method Detail |
---|
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 ;-))
getFlag
in interface IFlag<T>
getFlag
in class Flag<T>
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
setFlag
in interface IFlag<T>
setFlag
in class Flag<T>
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.
inSync
in interface IFlag<T>
inSync
in class Flag<T>
public boolean isFrozen()
Flag
Flag.defreeze()
is called.
isFrozen
in interface IFlag<T>
isFrozen
in class Flag<T>
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.
freeze
in interface IFlag<T>
freeze
in class Flag<T>
public void defreeze()
Flag
Flag.freeze()
for info.
defreeze
in interface IFlag<T>
defreeze
in class Flag<T>
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |