Results 1 to 7 of 7
Thread: Problem in threading
- 11-21-2008, 11:23 PM #1
Member
- Join Date
- Nov 2008
- Posts
- 2
- Rep Power
- 0
Problem in threading
Hi i am new to java..
i am facing some problem in synchronization. problem is as follows:
I have a class A with two variables x and y and their getter and setter methods defined in the class.
Another class B makes an object O of class A and start two threads T1 and T2.
Both T1 and T2 can modify O.
How do i take care of synchronization here.
If i say synchronized keyword with getter and setter functions defined in class A then it does not make sure that if T1 is reading O.x then T2 does not modify O.x or O as a whole.
Keyword synchronized just make sure that same block of code or method is accessed by one thread at a time.
I hope this explains my doubt. If not then kindly let me know.
Thanks
- 11-22-2008, 11:45 AM #2
Your doubt is clearly worded, some folks have failure to understand how this works - I have discovered an efficient approach:
not hard problem...but not often solved without work.Java Code:public variable accessValue(boolean set, int value){if(set)value = value;return value:}else{return value;}Introduction to Programming Using Java.
Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor
- 11-22-2008, 04:39 PM #3
Member
- Join Date
- Nov 2008
- Posts
- 2
- Rep Power
- 0
Thanks Nicholas for response.
I have one more here....
If one thread T1 is accessing getter method of an Object then is it possible for other threads to access setter method of object at the same time.
Object is instance of class which contains many variables of type int, double etc..
- 11-23-2008, 01:03 AM #4
use synchronized methods, probably all over the place. Especiallay get and set. When Thread(n) does access on a variable, the code I posted is the approach of choice, though not given in the reference works I have seen. There is also synchronized blocks, which can as well synchronize on some object. Be careful in general to place all of your instance variables in Objects, the compiler will tend to do it for you behind the scenes. If you have an int that changes often, I just write a utility class in the file that has get and set on an int, thus making it mutable.
Where exactly all this "Integer cannot be changed" is largely a consequence of large projects, a coder later can change something and that fouls up other work. You will see this a lot, and is close to the question you asked. What you can do is have get and set synchronize on the same object - Then only one method runs at a time, which one goes first will not be reliable no matter how much code you put on it, that is in part why I use the construct I posted.
Google for "Double Checked Locking is Broken" which will lead you to efficient discussions of what your question shows to be your immediate goal.
I enjoy Threading issues, most people will tell you to use Barrier class, and wait() { or sleep() and yield() } which is not what you are trying to achieve.Introduction to Programming Using Java.
Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor
- 11-26-2008, 04:23 PM #5
Member
- Join Date
- Nov 2008
- Posts
- 2
- Rep Power
- 0
If you want to synchronize around each variable separately, which sounds like what you are trying to do you can put separate sychronized("A"){} and syncrhonized("B") {} blocks around the code inside your getters and setters where the fields are read or written.
This way accessing first fields won't block access to the second:
Also you can use atomic objects which don't use mutexes for synchronization and are more efficient at concurrent access (see java.util.concurrent.atomic package).Java Code:int getA() { synchronized ("A") { return this.a; } } void setA(int v) { synchronized ("A") { this.a = v; } } int getB() { synchronized ("B") { return this.b; } } void setB(int v) { synchronized ("B") { this.b = v; } }Last edited by voytechs; 11-26-2008 at 04:25 PM.
- 11-26-2008, 11:30 PM #6
In general, voytechs shows the general approach, you can syncryonize(this){} if the variable is in the class - which obviously it would have to be to be a get or set method.
That would be where your " if T1 is reading O.x then T2 does not modify O.x or O as a whole." question actually ends up. For practical, we can say the get and set only modifiy one variable so "shift the state of the class" - well that's what you just did by changing a variable.
If you have to have several things boogie together, then use what is called a Lock Object.
These can be idiot simple, they seem to work and are canonical code for synchronized.Introduction to Programming Using Java.
Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor
- 12-01-2008, 08:16 PM #7
Member
- Join Date
- Nov 2008
- Posts
- 1
- Rep Power
- 0
Similar Threads
-
Threading issue
By Eku in forum New To JavaReplies: 2Last Post: 09-18-2008, 10:47 AM -
Threading in EJB
By java08 in forum Advanced JavaReplies: 2Last Post: 08-12-2008, 11:09 AM -
Threading a method
By Sephirangel in forum Threads and SynchronizationReplies: 3Last Post: 05-05-2008, 07:38 AM -
Java threading
By Eranga in forum Advanced JavaReplies: 2Last Post: 03-13-2008, 05:30 AM -
Threading prob..
By banie in forum Java AppletsReplies: 0Last Post: 02-05-2008, 06:30 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks