Results 1 to 2 of 2
Thread: synchronized statements query?
- 02-11-2011, 04:38 AM #1
Member
- Join Date
- Aug 2009
- Posts
- 26
- Rep Power
- 0
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."
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 ifJava Code:public void addName(String name) { synchronized(this) { lastName = name; nameCount++; } 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)Java Code:nameList.add(name)
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
- 02-11-2011, 05:26 PM #2
Senior Member
- Join Date
- Feb 2011
- Posts
- 118
- Rep Power
- 0
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
-
Problems with synchronized
By BigHotCuppa in forum Threads and SynchronizationReplies: 3Last Post: 11-20-2010, 08:11 PM -
Difference b/w "synchronized","synchronize",and "synchronized()"
By Bala_Rugan in forum New To JavaReplies: 1Last Post: 09-08-2010, 04:08 PM -
help regarding synchronized threads
By vishalrimt in forum Threads and SynchronizationReplies: 4Last Post: 08-08-2010, 12:47 PM -
Synchronized(this)?
By kiza in forum Threads and SynchronizationReplies: 10Last Post: 04-07-2009, 01:20 PM -
synchronized
By bugger in forum New To JavaReplies: 2Last Post: 11-28-2007, 10:33 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks