Results 1 to 8 of 8
  1. #1
    veronique is offline Member
    Join Date
    Apr 2011
    Posts
    22
    Rep Power
    0

    Default Is this thread safe?

    Is this implementation thread safe? How do I make it thread safe? It seems to be working when I run it, but I'm not sure if it's correct.

    public class Calculate {

    private int[] list = {0, 0};

    public synchronized String getElements()
    {
    return list;
    }

    public synchronized void setElements(int first, int second) {
    list[0] = first;
    list[1] = second;
    }
    }



    thanks
    Last edited by veronique; 07-28-2011 at 09:23 PM.

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Looks to be.

  3. #3
    veronique is offline Member
    Join Date
    Apr 2011
    Posts
    22
    Rep Power
    0

    Default

    Sorry could you elaborate pls?

  4. #4
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    As in on what?

    You do realise, however, that the Dimension object you return is not thread-safe, however, right? As whoever retrieves it can change its coordinates themselves. You want to return a clone of the Dimension object is you want to prevent that.

  5. #5
    veronique is offline Member
    Join Date
    Apr 2011
    Posts
    22
    Rep Power
    0

    Default

    How would I return a clone? return this.coordinates?

  6. #6
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    As in with the clone method? Or as in creating a new one using the coordinates from the first?

  7. #7
    veronique is offline Member
    Join Date
    Apr 2011
    Posts
    22
    Rep Power
    0

    Default

    using coordinates from the first.

    thanks

  8. #8
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default

    The Dimension class has a copy constructor. So instead of returning coordinates, return new Dimension(coordinates).
    Get in the habit of using standard Java naming conventions!

Similar Threads

  1. A collection is not thread-safe?
    By JUser in forum Advanced Java
    Replies: 1
    Last Post: 09-28-2010, 10:38 PM
  2. Singleton Thread safe class
    By userj2ee in forum Advanced Java
    Replies: 1
    Last Post: 08-13-2010, 06:32 PM
  3. Thread safe without using synchronization
    By swetu.vc in forum Threads and Synchronization
    Replies: 3
    Last Post: 01-20-2010, 09:06 AM
  4. Thread Safe Methods / Locks
    By mrhyman in forum Threads and Synchronization
    Replies: 16
    Last Post: 10-24-2008, 10:57 PM
  5. The safe way to stop a thread
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-09-2008, 07:31 PM

Posting Permissions

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