synchronized keyword

anyone have any idea what does the synchronized keyword means and wad it does? Thanks
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

   Synchronization is useful when you are trying to work in multi-threaded environment where concurrent requests can access/modify same object.

   Thread synchronization is done by setting locks on objects. This is similar to database locking except that all Java locks are exclusive locks. Other modes, such as shared locks or intention locks, are not supported. This seriously limits the level of concurrency possible in a Java program, and it also greatly increases the likelihood that a deadlock will occur.

   In Java, one does not explicitly set and release locks. This is done implicitly by executing a method declared to be synchronized or by executing a block that synchronizes on the object. When such a method or block starts, an exclusive lock is set on the object. When the method returns or the thread leaves the block, the lock is released. As in databases, a thread can hold the same lock more than once. This can happen if a synchronized method or block executes another synchronized method or block using the same object. The Java run-time system increments a lock count, exactly as in a database lock manager, each time a synchronized method or block is started, and decrements it each time a synchronized method returns or the thread leaves the block.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
zzynxSoftware engineerCommented:
synchronized   means:  the following method or compound statement will only be executed
                                  by ONE running thread at a time,
or                               the following class (object) 's methods will only be executen by
                                  ONE running thread at a time.
This only applied to programs with extra threads that might concurr at given times.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.