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...
Want your group to be featured on the Yahoo! Groups website? Add a group photo to Flickr.

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 #31428 of 32218 |
Re: [Java] Synchronized.


> ----- Original Message -----
> From: "Chooti Baba" <nibm963@...>
> To: "Java Official" <java_official@yahoogroups.com>
> Subject: [Java] Synchronized.
> Date: Wed, 16 Jul 2008 21:37:08 -0700 (PDT)
>
>
> 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();
> private Object lock2 = new Object();
>
> public void inc1() {
> synchronized(lock1) {
> c1++;
> }
> }
>
> public void inc2() {
> synchronized(lock2) {
> c2++;
> }
> }
> }
>
>
> 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".
>
>
>
>
>
> Best Regards & Good Luck
>
>
>
> Chooti Baba
>
>

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 in
lieu of any class,synchronized on lock doesn't allows any method that is also
synchronized to access the same object until it has monitor/control of that
locked object.

Base class reference can be assigned to any of its derived class reference so
Locking Object provides synchronisation for any of the classs' objects because
Object class is the base class for any class in Java.Hope it will help u.give
me reply if this is wrong or if u need further clarification.

--
Be Yourself @ mail.com!
Choose From 200+ Email Addresses
Get a Free Account at www.mail.com




Fri Jul 18, 2008 7:54 am

sankalp_s_sh...
Online Now Online Now
Send Email Send Email

Forward
Message #31428 of 32218 |
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