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...
Real people. Real stories. See how Yahoo! Groups impacts members worldwide.

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

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 is referred to as the "monitor"; it controls the
synchronization. Thus, in your example, you have 2 blocks:
synchronized (lock1) {
...
}

and

synchronized (lock2) {
...
}

Since these are different monitors, 2 threads can go through the code at
a time (one through each block). However, if both blocks were declared
synchronized (lock1) {
...
}
only one thread could execute one block or the other, but, both blocks
would never be active concurrently.

You might want to go over things further at java.sun.com .

-Java Guy


Chooti Baba wrote:
>
> 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
>
> [Non-text portions of this message have been removed]
>
>



Fri Jul 18, 2008 2:00 am

mmjavaguy
Offline Offline
Send Email Send Email

Forward
Message #31427 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