This has lock on a single object(this) so all the methods which are synchronized cannot be accessed by another thread
It has lock on an object which can be defined by you let's say a String. if u have four functions say put,get,push,pop & u want while pushing no thread shud be able to access pop but they can access put or get & vice versa then define single object for push & pop and another object for put & get.Remember lock is on an object For Example [/qb]<hr></blockquote>
[ edited to use separate code blocks rather than one big one - the text in between code was treated as an overly long line of code, messing up the display - Jim ]
[ July 17, 2006: Message edited by: Ela Jha ] [ July 17, 2006: Message edited by: Jim Yingst ]
The practical differences are in controlling scope and the monitor.
A synchronized method synchronizes the whole method (no surprise) while a block synchronizes only the block. You might have a longer method with some parts that need synchronized and others that don't. The block lets you sync only the exact lines that really need it.
A synchronized method synchronizes on the object instance or the class. These are very public monitors, meaning some other thread could synchronize on them for the wrong reason, leading to slowdowns or deadlocks. Maybe it's necessary to protect method A from multiple threads and method B from multiple threads, but it's ok if one thread is in A and another is in B. Synchronized methods can't do that, but blocks can by holding different object monitors.
Here's one way to synchronize on a private monitor:
Pop quiz: Why the normally wasteful "new String()" ? [ July 17, 2006: Message edited by: Stan James ]
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
[Stan]: Pop quiz: Why the normally wasteful "new String()" ?
Counterquiz: why a String here at all?
"I'm not back." - Bill Harding, Twister
Joined: Jan 29, 2003
I couldn't think of a way to give any other Object a meaningful value. "new Anything()" would do the job, but "new Integer(1)" would make readers ask, "Why 1?" Hey, that could be real fun. Let's make it
new ServerSocket("123.234.012.123", 1234); // DO NOT CHANGE THIS ADDRESS! [ July 17, 2006: Message edited by: Stan James ]
Personally I think the variable name is the only thing that's going to give it value in this context.
Joined: Jan 30, 2000
Yeah, I would normally just use
as that raises a minimal number of irrelevant questions. In the unlikely event I need to print something out for debugging purposes, I'd rather have Object's toString(), which will at least print different values for different objects. If I have two different locks that are both Strings that say "lock", I would have a harder time telling them apart than with plain Objects.
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com