Search the web
Sign In
New User? Sign Up
Java_Official · Java SE . EE . ME . AJAX . Web services
? Already a member? Sign in to Yahoo!

Yahoo! Groups Tips

Did you know...
Show off your group to the world. Share a photo of your group with us.

Best of Y! Groups

   Check them out and nominate your group.
Having problems with message search? Fill out this form to ensure your group is one of the first to be migrated to the new message search system.

Messages

  Messages Help
Advanced
Synchronized.   Message List  
Reply | Forward Message #31425 of 32213 |
Re: [Java] Synchronized.

Chooti Baba wrote:
> what is the meaning of the following statements:
> synchronized(lock1)
> synchronized(lock2)
>
>
> I have come across the keyword "synchronized" in blocks/methods,
> but this particular type of "declaration" is confusing me which
> I never came across. Can anyyone please explain me in
> "plain English" the purpose of having a reference within
> brackets next to the keyword "synchronized".
>

If you synchronize a method, the JVM locks on the object's monitor. This
prevents any thread except the one owning the lock from entering any
synchronized block in that object. However, you can localize the locks
to the bare minimum necessary to prevent deadlock and starvation. To do
this, you use the form of synchronization you see in that example.
Typically you will have a method 20-30 lines long and only a small
portion will be synchronized because in reality you are more concerned
about locking on a variable than the object as a whole. Additionally,
you can lock on objects other than "this" which is what you see here.
The objects are just regular Object instances that exist solely for
synchronization.

The Java 1.5 way to do this, however, is through the concurrency
framework. You would create an instance of ReentrantLock that behaves
similar to the "synchronized" keyword but is far more flexible and
offers superior performance (although still slower than code that does
not use locking). There are other locks but that one is probably what
most people need most of the time.

Additionally, there are atomic variable types. In this example you do
not need any locking or synchronization: you would simply create an
AtomicInteger and use its methods to alter it. If two threads happen to
modify it at the same time, the enclosing class guarantees that the
updates are atomic: you will not have the subtle issues that can happen
sometimes where you may lose an update and the counter only increments
once. Behind the scenes it uses locking, but this is transparent to the
user of the variable. You do not need to maintain the locks, it is
encapsulated for you.

I use the concurrency classes for all new code I write that needs to be
thread-safe. As I go through old code I at least do the bare minimum of
adding Lock classes rather than using the synchronized keyword. There
are more optimizations to be done but I prefer not to break code unless
I know I can fix it :-)

--
John Gaughan




Fri Jul 18, 2008 12:23 am

john23874
Offline Offline
Send Email Send Email

Forward
Message #31425 of 32213 |
Expand Messages Author Sort by Date

Hi Gurus, Please have a look at the following code: public class MsLunch { private long c1 = 0; private long c2 = 0; private Object lock1 = new Object(); ...
Chooti Baba
nibm963
Offline Send Email
Jul 17, 2008
7:58 pm

... If you synchronize a method, the JVM locks on the object's monitor. This prevents any thread except the one owning the lock from entering any synchronized...
John Gaughan
john23874
Offline Send Email
Jul 18, 2008
7:52 pm

The keyword "synchronized" means that only 1 thread at a time is allowed through that piece of code. The variable name in parens is the name of an object that...
Java Guy
mmjavaguy
Offline Send Email
Jul 18, 2008
7:52 pm

... As far as my knowledge is concerned synchronized(lock) performs lock on the lock object of type Object.lock is of type Object so that it can be referenced...
sankalp sharma
sankalp_s_sh...
Online Now Send Email
Jul 18, 2008
7:52 pm

Dear sankalp,                  ... From: sankalp sharma <sankalp@...> Subject: Re: [Java] Synchronized. To: Java_Official@yahoogroups.com ...
c. ramesh
cr_lotus
Offline Send Email
Jul 22, 2008
4:18 pm
Advanced

Copyright © 2009 Yahoo! Inc. All rights reserved.
Privacy Policy - Terms of Service - Guidelines - Help