This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
How can i gurantee that only one thread is accessing myMehtod() at a given time amoung other several threads.
Changing the code to this:
Will make sure only one thread can enter the method at a time. You can also do this to protect code inside the method:
In this sample multiple threads can enter the method but only one at a time could execute the // some code portion. There are many advantages to doing this.
These two options, in no way, ensure 100% thread safety of the method though. To do that you would have to: 1) Use method-local variables wherever possible 2) Use ThreadLocal values wherever possible 3) synchronize all access to non-method-local or thread-local data that the method uses. That means if you have the same variable used in 2 methods then both methods should be synchronized the same way (on the same lock). 4) Don't let the references to any objects in the method escape. Say for instance you have an ArrayList you use in the method. If that ArrayList is passed into the method, or is a class variable that other code can access, then some external code has access to the list and can modify it in an unsafe manner. So your code would no longer be Thread safe. 5) Any objects that your objects reference must also not be allowed to escape. For example if you have an ArrayList and you successfully protect the List from escaping, if the objects it stores were passed into your thread-safe code from external code, and those objects can be modified then they can lead to unsafe changes and your method is no longer thread-safe.
I think this covers most things, but if not I am sure others will correct me.
Solving 1, 2, and 3 should be self-explanatory or could be looked up in books. To solve 4 you should use defensive copying - Make a copy of all modifiable data that comes into your thread-safe code, and make a copy of all modifiable data that leaves your thread-safe code. That way the thread-safe portions only works on a copy of the incoming data and any external modification of the external data doesn't disrupt the thread-safe state. For 5, you do the same as for 4, and you try to use immutable objects wherever you can, and defensive copies for anything you can't.
It looks to me like all the solutions so far prevent more than one thread from accessing the method using the same instance. Often this is what you want, but it isn't what you said. If you really want to make sure only one thread is executing a method, period, regardless of which instance you use, then you need something like this:
Various other options are possible as well - but what they all have in common is that they all sync or lock on some instance which is shared across the entire class - either the DualSync.class instance (which is a single instance of the class Class), or some other statically-held object.
You need this type of class-level synchronization if you are modifying class-level data - i.e. static fields and the objects they refer to. If you are not accessing any static mutable data, but you are accessing instance data (data held by non-static fields), then you need the more conventional instance-level synchronization described in the previous posts.
Also, another way to achieve 100% thread safety is to make sure your method uses only immutable objects. Often this isn't really possible in Java (or more accurately, it may be overly inefficient or complex - but give it a try. In many cases it may be the easiest way, by far, to achieve thread safety.
subject: how can i achieve a 100% thread safty method ?