How Can I Achieve A 100% Thread Safty Method - (Threads Forum at JavaRanch)
How Can I Achieve A 100% Thread Safty Method - (Threads Forum at JavaRanch)
how can i achieve a 100% thread safty method ? (Threads forum at JavaRanch)
Java FAQ
Recent Topics
Register / Login
A special promo: Enter your blog post or vote on a blogger to be featured in an upcoming Journal
Author
Rahul Shilpakar Ranch Hand Joined: Aug 29, 2006 Posts: 132
Hi all, How can i achieve the 100% thread safe method of a class? e.g. in the following code i want to make myMethod() 100% thread safe. How can i gurantee that only one thread is accessing myMehtod() at a given time amoung other several threads. what sort of arrangement shall i have to do with code?
view plain
c opy to c lipboard
N ote: T ext c ontent in the c ode bloc ks is automatic ally word- wrapped
0 1 . 0 2 . 0 3 . 0 4 . 0 5 . 0 6 . 0 7 . 0 8 .
c l a s sD u a l S y n c h { v o i dm y M e t h o d ( ) { / /s o m ec o d e } }
www.coderanch.com/t/234394/threads/java/achieve-thread-safty-method
1/5
6/14/13
how can i achieve a 100% thread safty method ? (Threads forum at JavaRanch)
I gone through lot of tutorial books but getting confused in Locking and other concepts. Guid me. Thank you in advance.
victor kamat Ranch Hand Joined: Jan 10, 2007 Posts: 247
This code will do it -class DualSynch { private final Object lock = new Object(); void myMethod() { synchronized(lock) { // code } } }
How can i gurantee that only one thread is accessing myMehtod() at a given time amoung other several threads.
N ote: T ext c ontent in the c ode bloc ks is automatic ally word- wrapped
0 1 . 0 2 . 0 3 . 0 4 . 0 5 . 0 6 . 0 7 .
c l a s sD u a l S y n c h { s y n c h r o n i z e dv o i dm y M e t h o d ( ) { / /s o m ec o d e } }
Will make sure only one thread can enter the method at a time. You can also do this to protect code inside the method:
view plain c opy to c lipboard print ?
N ote: T ext c ontent in the c ode bloc ks is automatic ally word- wrapped
www.coderanch.com/t/234394/threads/java/achieve-thread-safty-method
2/5
6/14/13
how can i achieve a 100% thread safty method ? (Threads forum at JavaRanch)
0 1 . 0 2 . 0 3 . 0 4 . 0 5 . 0 6 . 0 7 . 0 8 . 0 9 . 1 0 .
c l a s sD u a l S y n c h { p r i v a t eO b j e c tl o c k=n e wO b j e c t ( ) ; v o i dm y M e t h o d ( ) { s y n c h r o n i z e d ( l o c k ){ / /s o m ec o d e } } }
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.
Steve
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
3/5
www.coderanch.com/t/234394/threads/java/achieve-thread-safty-method
6/14/13
how can i achieve a 100% thread safty method ? (Threads forum at JavaRanch)
Posts: 2804
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:
view plain c opy to c lipboard print ?
N ote: T ext c ontent in the c ode bloc ks is automatic ally word- wrapped
0 1 . 0 2 . 0 3 . 0 4 . 0 5 .
c l a s sD u a l S y n c h{ s t a t i cs y n c h r o n i z e dv o i dm y M e t h o d ( ){ / /s o m ec o d e } }
or this:
view plain c opy to c lipboard print ?
N ote: T ext c ontent in the c ode bloc ks is automatic ally word- wrapped
0 1 . 0 2 . 0 3 . 0 4 . 0 5 . 0 6 . 0 7 .
c l a s sD u a l S y n c h{ v o i dm y M e t h o d ( ){ s y n c h r o n i z e d ( D u a l S y n c . c l a s s ){ / /c o d e } } }
or this:
view plain c opy to c lipboard print ?
N ote: T ext c ontent in the c ode bloc ks is automatic ally word- wrapped
0 1 . 0 2 . 0 3 . 0 4 . 0 5 . 0 6 . 0 7 . 0 8 . 0 9 .
c l a s sD u a l S y n c h{ p r i v a t es t a t i cf i n a lO b j e c tl o c k=n e wO b j e c t ( ) ; v o i dm y M e t h o d ( ){ s y n c h r o n i z e d ( l o c k ){ / /c o d e } } }
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 nonstatic 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
www.coderanch.com/t/234394/threads/java/achieve-thread-safty-method 4/5
6/14/13
how can i achieve a 100% thread safty method ? (Threads forum at JavaRanch)
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.
Similar Threads Static Methods in Java cant use out.println() inside a method declaration JavaCaps mock 1 Q:9 synchronization Static and Instance Blocks..
All times above are in your local time zone & format.T he current ranch time (not your local time) is Jun 14, 2013 01:44:18 .
www.coderanch.com/t/234394/threads/java/achieve-thread-safty-method
5/5