Is there a pthread_once equivalent in Java? - java

In C/C++ world, it is very easy make a routine executed just once by using pthread_once. In Java, I generally use static atomic variables to do the explicit check if the routine was run already. But that looks ugly and hence wondering if there is something like pthrea_once in Java.

Since you refer to “static atomic variables” you seem to talk about static resources which do not need special actions if you initialize them within the class initializer itself:
class Foo {
static ResourceType X = createResource();
}
Here, createResource() will be executed exactly once in a thread-safe manner on the first use of Foo, e.g. when Foo.X is accessed the first time. Threads accessing X while the class initialization is in progress are forced to wait, but subsequent access will be performed without any synchronization overhead. Typically, but not necessarily, the variable will be declared final as well.
If you have multiple resources whose creation should be deferred independently, the owner class might use inner classes, each of them holding one resource.
If your question is about an action which should be executed exactly once without returning a value, the static initialization can be used as well. You only have to add a member you can access to trigger the class initialization, e.g.:
class Foo {
static { performAction(); }
static void performActionOnce() {}
}
Here, calling Foo.performActionOnce() will cause performAction() to be executed the first time while all other subsequent invocations do nothing. You can also rely on that on returning from performActionOnce() the action within performAction() has been completed, even when there is contention on the first invocation.
This is different from any atomic variable approach as atomic variables do not provide a sufficient waiting capability for the case that the first invocation is contended. If you combine the atomic variable with a waiting queue, you end up what Lock (or any other AQS based concurrency tool) provides. For instance variables where the static initialization does not work, there is no simple workaround (besides thinking about whether initialization really has to be lazy).

Related

Java lambda expression with local variable capture

The reason for a local variable to be final or effectively final this because of concurrency issues. In the jls 8 specification, it states the following.
The restriction to effectively final variables prohibits access to
dynamically-changing local variables, whose capture would likely
introduce concurrency problems.
All good and sweet, but I did a little experiment. What if I synchronize the method, that would eliminate the possibility of dynamically-changing local variable because I am guaranteed only a single thread can execute this code. But the compile threw an error saying it has to be final or effectively final.
Is logic right?
Consider the following code:
public synchronized void capture() {
int localVariable = 100;
Interf i = (text) -> System.out.println(text + localVariable);
i.m1("This local variable is: ");
localVariable = 1000;
}
}
The answer is simply that your variable goes out of scope at the end of the method. This is easily solved with effectively final variables as the compiler just copies the value into the lambda. Since the code in the lambda expression can also be run outside of the method (where the modifiable variable is garbage collected already) this won't work. You also can't expect the compiler to somehow make a copy of your variable and then dynamically change it when it's modified outside of your lambda expression. I hope that clears it up.
But the compile threw an error saying it has to be final or effectively final.
That's because it does as per the rules. No it's, no buts; it doesn't matter if you've actually guarded against all concurrency issues or not - if it's not effectively final, it won't compile.
In your simplistic example here it's probably ok. However, making the method synchronized is irrelevant since local variables will always be tied to their per-thread invocation anyway. It's threading issues within the context of the method itself that the compiler is worried about, and that can easily happen with lambdas being used (which may be executed some arbitrary time in the future, after the state of a non-final variable may have changed, and if it has, it's not at all clear what state should be used - the initial state, or the updated state.)
Imagine the lambda that you have creates a CompletableFuture to be executed by the ForkJoinPool or another executor?
That is why synchronized on this method would not suffice to overrule the rule of having the local variable effectively final. The lambda will execute synchronously and will be synchronized but the async task that it creates would not.

Read Indirect state Vs. Read Direct state of a variable in java

In following code compilation fails for static variable j inside the static block, as mentioned in comment.
However, It is working fine inside the method m1()
class StaticBlock {
static {
m1();
//compilation fails because variable are in read indirect state
System.out.println(j);
}
static void m1() {
System.out.println(j);
}
static int j = 10;
I know root cause of compilation failure - variable j is in Read Indirect State.
My question- Why is this behavior, we can also print 0 inside static block as we are doing in m1().
What made API developers to have this discrepancy
Why is this behavior, we can also print 0 inside static block as we
are doing in m1().
What made API developers to have this discrepancy
There are competing priorities revolving around simple specifications for the order of events during class initialization, consistency of constant (i.e. final) class fields, programmer expectations, and ease of implementation.
The values of constant fields provides a good starting point. Java would like to avoid the default values of such fields being observable, and especially to avoid their default values being used in the initialization of other class variables. Therefore, these are initialized first, before static initializer blocks or the initializers of other class variables. They are initialized in the order they appear in the source code, which is a rule that is easy for both humans and compilers to understand. But that affords the possibility that the initializer of one class variable sees the default value of another, yielding surprising, unwanted results. Java therefore specifies that that case must be detected and rejected at compile time.
Static initializer blocks and the initializers of other class variables are executed afterward, in the order they appear in the source code. The case for the constraint you're asking about is not as strong here, but it's reasonable to choose consistency by applying the same rule here as is applied to class constants. Combined, the effect is to have easy to understand and predict initialization order that is also mostly consistent with a model of class variables' initializers being evaluated and assigned before static initializer blocks are evaluated.
But then come static methods. It is highly desirable for static methods to be available for use during initialization, but they are also usable after initialization is complete, when none of the initialization-order considerations are relevant. It is therefore unfeasible to restrict static methods' access to variables based on order of appearance in source code. Conceivably, the VM could instead be required to keep track of class variables' individual initialization state, either by control-flow analysis at compile time or by some form of runtime monitoring, but rather than require such complexities, Java opts for simplicity, allowing people who insist on making a mess (by observing default values of class variables) to do so.
I emphasize, finally, that so-called "Read Indirect Write Only state" is part of a third-party model of how this all works. Java itself has no such concept -- such a thing is exactly what it rejects in favor of simplicity when it comes to requirements on static methods' use of class variables.

Static method in multi threaded application

I have a function in a class, which just masks the input card number. I have made it synchronized because, i do not want more than one thread calling my maskcard function simultaneously. My question is, should i make this function static to be more efficient and clean? Now, where ever i am calling my maskcard function, i am calling on an instance.
public class CardMasker {
public synchronized String maskCardNumber(String cardNumber)
{
..
..
}
}
A better solution would be to make sure that you have only one CardMasker instance and use non-static synchronized maskCardNumber.
I you keep the method instance specific i.e. the way you have implemented it currently :
public synchronized String maskCardNumber(String cardNumber)
Here all the threads working on same instance will access the method in synchronized fashion.
If you want to make it static synchronized, here are the points to be considered:
Does it perform operation which needs to be accessed in synchronized fashion irrespective of any instance of this class. If yes, then there is no point keeping it instance specific, because threads using different instances of this class will still be able to call the method simultaneously.
My question is, should i make this function static to be more efficient and clean?
No. It won't make it more efficient. The difference in speed (if there is any at all) will be negligible.
Furthermore, if you then make the method synchronized, you are creating a concurrency bottleneck. This doesn't need to happen in the non-static case; for example if you resist the temptation to "save space" (or something) by using a singleton. Indeed, if each thread has its own thread-confined instance of the class that implements this helper method, then the method probably doesn't need to be synchronized at all.
Also, it is a matter or opinion, but static methods are not more "clean" than instance methods.

In Java, do methods that don't use static or class variables need to be synchronized?

Do methods that only use local variables inside suffer any threading issues ?. Somewhere it was mentioned that the method with local variables are copied to each thread stack frame to work with and do not need to synchronized for multithreaded implementation unless it uses class level or static references/variables ?
If your method only operates on parameters and locally-defined (as opposed to class member) variables then there are zero synchronization problems to worry about.
But...
This means any mutable reference types you use must live and die only within the scope of your method. (Immutable reference types aren't a problem here.) For example this is no problem:
int doSomething(int myParameter)
{
MyObject working_set = new MyObject();
interim = working_set.doSomethingElse(myParameter);
return working_set.doSomethingElseAgain(interim);
}
A MyObject instance is created within your method, does all of its work in your method and is coughing up blood, waiting to be culled by the GC when you exit your method.
This, on the other hand, can be a problem:
int doSomething(int myParameter)
{
MyObject working_set = new MyObject();
interim = working_set.doSomethingElse(myParameter);
another_interim = doSomethingSneaky(working_set);
return working_set.doSomethingElseAgain(another_interim);
}
Unless you know for sure what's going on in doSomethingSneaky(), you may have a need for synchronization somewhere. Specifically you may have to do synchronization on the operations on working_set because doSomethingSneaky() could possibly store the reference to your local working_set object and pass that off to another thread while you're still doing stuff in your method or in the working_set's methods. Here you'll have to be more defensive.
If, of course, you're only working with primitive types, even calling out to other methods, passing those values along, won't be a problem.
Does methods that only use local variables inside, do not suffer any threading issues ?
True in a very simplistic sense, but lets be clear - I think this is only true if:
such a method uses only local variables that are primitives or references to mutable instances that cannot otherwise be accessed outside the method by any other means.
such a method invokes only methods that are thread-safe.
Some ways these rules could be violated:
A local variable could be initialized to point to an object that is also accessible outside the method. For example, a local variable could point to a singleton (Foo bar = Foo.getSingleton()).
A local instance held by a local variable could "leak" if the instance is passed as a argument to an external method that keeps a reference to the instance.
A class with no instance variables and with only a single method with no local variables could still call the static method of another class that is not thread-safe.
The question is very generic, so please do not expect any specificity from my answer.
1_ We need to more careful with static methods than say instance methods.
2_ #Justmycorrectopinion is about right, but some of the terms he described needs to be more elaborated to be perfect. ( Even if the static method, only works on local variable, there is still possibility of race condition.)
3_ For me there are simple rules that have helped me analyze thread safety.
Understand if each components encapsulated within it is shareable or not. So the simplest solution is to reduce the scope of all variable and only increase scope if absolutely necessary and if component perform mutation on a object, its usually not thread safe.
4_ Use tooling support to perform static code analysis on thread safety. (Idea has checkthread plugin).
5_ Never use static method to perform object mutation. If calling static variable causes object mutation, then the developer is just circumventing OOPS.
6_ Always document thread safety. Remember some method may not need to be synchronized when you develop, but can be made not thread safe very easily.
7_ Last but probably my most important point, make sure most of your objects are immutable. In my experience, most of the time, I never had to make many of my objects mutable. (In rare cases when object state needs to be changed, defensive copying / New Object Creation is almost always better. )
You do not need to worry about local variables. Instance variables however are something to care about.

Threads and synchronisation

I've problem understanding the following piece of code:-
public class SoCalledSigleton{
private final static boolean allDataLoaded = SoCalledSigleton();
private SoCalledSigleton(){
loadDataFromDB();
loadDataFromFile();
loadDataAgainFromDB();
}
}
Is this piece of code thread safe? If not then Why?
This will create an error in Java.
private final static boolean allDataLoaded = SoCalledSigleton();
You're assigning an object to a boolean variable.
You forgot to add new to instantiate the variable.
But if your code is like this
public class SoCalledSigleton{
private final static SoCalledSigleton allDataLoaded = new SoCalledSigleton();
private SoCalledSigleton(){
loadDataFromDB();
loadDataFromFile();
loadDataAgainFromDB();
}
}
It is thread-safe as static initialization and static attributes are thread-safe. They are initialized only once and exists throughout the whole life-cycle of the system.
The code is unusable in its current form, so any notions of thread safety are irrelevent.
What public interface would users use to get an instance of the singleton?
(I assume that allDataLoaded is meant to be a SoCalledSigleton and boolean is just a typo :-)
If the class has no other constructors, or the loadData* methods don't do funny business (such as publishing this), its initialization is thread safe, because the initialization of final static data members is guarded by the JVM. Such members are initialized by the class loader when the class is first loaded. During this, there is a lock on the class so the initialization process is thread safe even if multiple threads try to access the class in parallel. So the constructor of the class is guaranteed to be called only once (per classloader - thanks Visage for the clarification :-).
Note that since you don't show us the rest of the class (I suppose it should have at least a static getInstance method, and probably further nonstatic members), we can't say anything about whether the whole implementation of the class is thread safe or not.
From what we can see, there are no specific issues - it's guaranteed that the constructor will only ever by called once (so by definition can't be run multithreaded), which I presume is what you were concerned about.
However, there are still possible areas for problems. Firstly, if the loadData... methods are public, then they can be called by anyone at any time, and quite possibly could lead to concurrency errors.
Additionally, these methods are presumably modifying some kind of collection somewhere. If these collections are publically accessible before the constructor returns, then you can quite easily run into concurrency issues again. This could be an issue with anything exception updating instance-specific fields (static fields may or may not exhibit this problem depending where they are defined in the file).
Depending on the way the class is used, simply writing all of the data single-threaded may not be good enough. Collection classes are not necessarily safe for multi-threaded access even if read-only, so you'll need to ensure you're using the thread-safe data structures if multiple threads might access your singleton.
There are possibly other issues too. Thread-safety isn't a simple check-list; you need to think about what bits of code/data might be accessed concurrently, and ensure that appropriate action is taken (declaring methods synchronized, using concurrent collections, etc.). Thread-safety also isn't a binary thing (i.e. there's no such thing as "thread safe" per se); it depends how many threads will be accessing the class at once, what combinations of methods are thread-safe, whether sequences of operations will continue to function as one would expect (you can make a class "thread safe" in that is doesn't crash, but certain return values are undefined if pre-empted), what monitors threads need to hold to guarantee certain invariants etc.
I guess what I'm trying to say is that you need to think about and understand how the class is used. Showing people a snapshot of half a file (which doesn't even compile), and asking them to give a yes/no answer, is not going to be beneficial. At best they'll point out some of the issues for you if there are any; at worst you'll get a false sense of confidence.
Yeah, it's thread safe. The "method" is the constructor, and it will be called when the class is loaded, i.e. exactly once.
But looking at the stuff being done, I think it's probably a lousy idea to call it from the class loader. Essentially, you'll end up doing your DB connection and stuff at the point in time when something in your code touches the SoCalledSingleton. Chances are, this will not be inside some well-defined sequence of events where, if there's an error you have catch blocks to take you to some helpful GUI message handling or whatever.
The "cleaner" way is to use a synchronized static getInstance() method, which will construct your class and call its code exactly when getInstance() is called the first time.
EDIT: As The Elite Gentleman pointed out, there's a syntax error in there. You need to say
private final static SoCalledSingleton allDataLoaded = new SoCalledSigleton();

Categories

Resources