Results 1 to 2 of 2
  1. #1
    castiel is offline Member
    Join Date
    Aug 2009
    Posts
    26
    Rep Power
    0

    Default synchronized statements query?

    Hey,

    I've been trying to learn about synchronized statments and read the following
    link

    Intrinsic Locks and Synchronization (The Java™ Tutorials > Essential Classes > Concurrency)

    "In this example, the addName method needs to synchronize changes to lastName and nameCount, but also needs to avoid synchronizing invocations of other objects' methods. (Invoking other objects' methods from synchronized code can create problems that are described in the section on Liveness.) Without synchronized statements, there would have to be a separate, unsynchronized method for the sole purpose of invoking nameList.add."

    Java Code:
    public void addName(String name) {
        synchronized(this) {
            lastName = name;
            nameCount++;
        }
        nameList.add(name);
    }
    I get the first part of that paragraph but I can't get my head around what it's saying in the section I've bolded and underlined? Is it trying to say that if
    Java Code:
    nameList.add(name)
    was inside the synchronized block then that method itself wouldn't be able to be invoked by other methods until the object calling nameList.add(name) was finished? (this is alll based on the above code and if nameList.add(name) was contained within the synchronized block of code)


    So for an example of how I'm interpreting this, if p called addName() then
    p2 called add(), then p2 would be blocked from calling add() until the add() method invoked by p had finished execution?

    Java Code:
    public static void main(String[] args) {
    Person p = new Person();
    Person p2  = new Person();
    p.addName("Harry");
    
    p2.add("John");
    
    }

    Please help me understand this as simply as possible :)

    Cheers,
    newbie

  2. #2
    NRitH is offline Senior Member
    Join Date
    Feb 2011
    Posts
    118
    Rep Power
    0

    Default

    I can see how that's confusing. The problem isn't with calling other objects' methods within your synchronized block or method; it's really just that you should do as little as you need to do within your block or method so that any objects in other threads can access those same resources quickly. In the example given in the tutorial, there may be multiple threads trying to add names simultaneously. To prevent them from colliding with each other, you synchronize the block so that only one thread can do it at a time. After that, you add it to a name list, which itself had better be synchronized to avoid the same kinds of conflicts. Note that this is an issue ONLY if you're using multiple threads; in a single-threaded application, it's impossible for this kind of simultaneous access.

Similar Threads

  1. Problems with synchronized
    By BigHotCuppa in forum Threads and Synchronization
    Replies: 3
    Last Post: 11-20-2010, 09:11 PM
  2. Replies: 1
    Last Post: 09-08-2010, 05:08 PM
  3. help regarding synchronized threads
    By vishalrimt in forum Threads and Synchronization
    Replies: 4
    Last Post: 08-08-2010, 01:47 PM
  4. Synchronized(this)?
    By kiza in forum Threads and Synchronization
    Replies: 10
    Last Post: 04-07-2009, 02:20 PM
  5. synchronized
    By bugger in forum New To Java
    Replies: 2
    Last Post: 11-28-2007, 11:33 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
  •