If a single instance of class is created using singleton pattern, if it is not referenced for a long time, if a GC finds an unrooted tree whose leaf is the Singleton instance, will it be garbage collected?
Well, singleton pattern is implemented by defining a private static field, a private construtor and a static method that returns the field. So, the question boils down to: a static field is garbaged collected?
The answer is no, at least according to this SO answer: Do static members ever get garbage collected?
So, even if there is no other references to the static field it will not be GC'ed.
Related
I am trying to understand if there could be any issues with Predicate defined at class level in multithreaded application. ? We have defined such predicates in our services and using them up different methods of same class. Currently we have not seen any issue but I am curious to understand our class level Predicate object is going to function. will there be any inconsistency in the behaviour?
eg:
class SomeClass{
Predicate<String> check = (value) -> value.contains("SomeString");
// remaning impl. of the class.
}
The predicate in your example is categorically thread-safe. It is calling a method on an intrinsicly thread-safe (and immutable) object.
This does not generalize to all predicates though. For example
Predicate<StringBuilder> check = (value) -> value.indexOf("SomeString") >= 0;
is not thread-safe. Another thread could mutate the contents of the StringBuilder argument while this predicate is checking it. The predicate could also be vulnerable to memory model related inconsistencies.
(The StringBuilder class is not thread-safe; see javadoc.)
It is not clear what you mean by "class level". Your example shows a predicate declared as a regular field, not a static (class level) field.
With a variable declared as a (mutable) instance field, it is difficult to reason about the thread-safety of the field in isolation. This can be solved by declaring the field as final.
The documentation states that:
The nested class describes an object whose construction ensures that
the standard iostreams objects are properly constructed, even before
the execution of a constructor for an arbitrary static object.
As seen at:
https://msdn.microsoft.com/en-gb/library/fbyc90zw.aspx
But since static objects have an undefined init ordering how does ios_base::Init ensure that it runs before them?
I would hazard that the order of initialization of static variables is undefined, but the order of constructor calls for creation of an object/variable (static or not) is well defined.
The ios_base::Init class is nested in the ios_base class. So when any ios_base instance is constructed, the ios_base::Init constructor is run as well.
It doesn't matter which of the possible static instances of an object using ios_base exist or what order they run in. All that matters is that the ios_base::Init gets to run and initialize the standard streams first (all other constructor calls to ios_base::Init likely do nothing since the work is already done by the first constructor call).
My intention was to create a singleton, but do not know how to handle this in Rust, I have read this and this, but not sure if that's the way to create a simple singleton because one speaks of a "mutable singleton" and the other of a "safe-static-singleton".
A singleton is just a lazily initialized piece of static data. That means you really do want lazy-static.
Note that
For a given static ref NAME: TYPE = EXPR;, the macro generates a unique type that implements Deref<TYPE> and stores it in a static with name NAME.
which means NAME is actually akin to the constructor of the "singleton", and &*NAME is the "singleton" itself.
Can any one help me in finding the basic difference between mutable and immutable?
Immutable means that once initialized, the state of an object cannot change.
Mutable means it can.
For example - strings in .NET are immutable. Whenever you do an operation on a string (trims, upper casing, etc...) a new string gets created.
In practice, if you want to create an immutable type, you only allow getters on it and do not allow any state changes (so any private field cannot change once the constructor finished running).
A very basic definition would be:
Mutable Objects: When you have a reference to an instance of an object, the contents of that instance can be altered
Immutable Objects: When you have a reference to an instance of an object, the contents of that instance cannot be altered
Immutable means "cannot be modified after it is created".
An immutable type has a constructor and getters but not setters.
A mutable type can also have setters.
An example of an immutable type is DateTime. The method AddMinutes does not modify the object - it creates and returns a new DateTime.
Another example is string. For a mutable class similar to string you can use the class StringBuilder.
There is no keyword in C# to declare a type as immutable. Instead you should mark all member fields as readonly to ensure that they can only be set in the constructor. This will prevent you from accidentally modifying one of the fields, breaking the immutability.
An immutable type cannot be changed once instantiated. For example strings are immutable. Every time you want to change the value of a string a new instance is created.
Immutable variables using in functional languages.
Using a term variable is inappropriate and functional programmers
prefer the term value.
Advantages of Immutability:
1 Thread Safety
2 Sharing
3 Less error prone
So prefer immutability, if you have the choice. :)
Use Reference
imaginationhunt.blogspot
IS STRING MUTABLE OR IMMUTABLE IN .NET?
Mutable: Mutable means whose state can be changed after it is created.
Immutable: Immutable means whose state cannot be changed once it is created.
String objects are 'immutable' it means we cannot modify the characters contained in string also operation on string produce a modified version rather than modifying characters of string.
Is there a good, platform-agnostic way to implement a variable that's local to both a thread and a class instance, i.e. if you have T threads and I class instances, you have TxI instances of that variable? I'm using the D programming language, version 2, but a good language-agnostic answer would also be useful.
Here are some constraints:
Must never require synchronization. This rules out having a hash table mapping thread ID to variable reference as a member variable.
Must not keep references around that should be garbage-collected. This rules out having a thread-local, static hash table indexed by class instance.
Initialization should be lazy for efficiency. If a thread never accesses a given instance's variable then it should never be created.
Must not keep references around that should be garbage-collected. This rules out having a thread-local, static hash table indexed by class instance.
Use a hashtable with weak-referenced keys. Won't prevent garbage collection, and will drop the information from the hashtable when the key (the class instance) is collected.