Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-22-2008, 12:23 AM
Member
 
Join Date: Nov 2008
Posts: 2
Rep Power: 0
saurabh is on a distinguished road
Default 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
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 11-22-2008, 12:45 PM
Nicholas Jordan's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Southwest
Posts: 1,018
Rep Power: 3
Nicholas Jordan is on a distinguished road
Default
Your doubt is clearly worded, some folks have failure to understand how this works - I have discovered an efficient approach:
Code:
 public variable accessValue(boolean set, int value){if(set)value = value;return value:}else{return value;}
not hard problem...but not often solved without work.
__________________
Introduction to Programming Using Java.
Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 11-22-2008, 05:39 PM
Member
 
Join Date: Nov 2008
Posts: 2
Rep Power: 0
saurabh is on a distinguished road
Default
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..
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 11-23-2008, 02:03 AM
Nicholas Jordan's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Southwest
Posts: 1,018
Rep Power: 3
Nicholas Jordan is on a distinguished road
Default
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
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 11-26-2008, 05:23 PM
Member
 
Join Date: Nov 2008
Posts: 2
Rep Power: 0
voytechs is on a distinguished road
Default
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:

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;
 }
}
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).

Last edited by voytechs; 11-26-2008 at 05:25 PM.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 11-27-2008, 12:30 AM
Nicholas Jordan's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Southwest
Posts: 1,018
Rep Power: 3
Nicholas Jordan is on a distinguished road
Default
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
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 12-01-2008, 09:16 PM
Member
 
Join Date: Nov 2008
Posts: 1
Rep Power: 0
seenimurugan is on a distinguished road
Default hi everyone,,,
nice forum.. thanks..
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Threading issue Eku New To Java 2 09-18-2008 11:47 AM
Threading in EJB java08 Advanced Java 2 08-12-2008 12:09 PM
Threading a method Sephirangel Threads and Synchronization 3 05-05-2008 08:38 AM
Java threading Eranga Advanced Java 2 03-13-2008 06:30 AM
Threading prob.. banie Java Applets 0 02-05-2008 07:30 AM


All times are GMT +2. The time now is 09:19 AM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org