Results 1 to 7 of 7
  1. #1
    saurabh is offline Member
    Join Date
    Nov 2008
    Posts
    2
    Rep Power
    0

    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

  2. #2
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    7

    Default

    Your doubt is clearly worded, some folks have failure to understand how this works - I have discovered an efficient approach:
    Java 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

  3. #3
    saurabh is offline Member
    Join Date
    Nov 2008
    Posts
    2
    Rep Power
    0

    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..

  4. #4
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    7

    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

  5. #5
    voytechs is offline Member
    Join Date
    Nov 2008
    Posts
    2
    Rep Power
    0

    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:

    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;
     }
    }
    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 04:25 PM.

  6. #6
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    7

    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

  7. #7
    seenimurugan is offline Member
    Join Date
    Nov 2008
    Posts
    1
    Rep Power
    0

Similar Threads

  1. Threading issue
    By Eku in forum New To Java
    Replies: 2
    Last Post: 09-18-2008, 10:47 AM
  2. Threading in EJB
    By java08 in forum Advanced Java
    Replies: 2
    Last Post: 08-12-2008, 11:09 AM
  3. Threading a method
    By Sephirangel in forum Threads and Synchronization
    Replies: 3
    Last Post: 05-05-2008, 07:38 AM
  4. Java threading
    By Eranga in forum Advanced Java
    Replies: 2
    Last Post: 03-13-2008, 05:30 AM
  5. Threading prob..
    By banie in forum Java Applets
    Replies: 0
    Last Post: 02-05-2008, 06:30 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •