[Webinar] Learn how to a build a cloud-first strategyRegister Now


synchronized keyword

Posted on 2004-11-30
Medium Priority
Last Modified: 2010-04-16
anyone have any idea what does the synchronized keyword means and wad it does? Thanks
Question by:eturfclub

Accepted Solution

mjmohan earned 225 total points
ID: 12712540
   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.

LVL 37

Expert Comment

ID: 12713697
LVL 24

Expert Comment

ID: 12713705
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.

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Suggested Courses
Course of the Month20 days, 12 hours left to enroll

865 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question