What does 'synchronized' mean in JAVA ?

Posted By: Matpal - June 08, 2011
Synchronized means that in a multiple threaded environment, a synchronized object does not let two threads access a the same time. This means that one thread can't be reading while another updates it.

The second thread will instead wait until the first is done. The overhead is speed, but the advantage is guaranteed consistency of data.

For Example :

When you have two threads that are reading and writing to the same 'resource', say a variable named foo, you need to ensure that these threads access the variable in an atomic way. Without the synchronized keyword, your thread 1 may not see the change thread 2 made to foo, or worse, it may only be half changed. This would not be what you logically expect.


Post a Comment

Note: Only a member of this blog may post a comment.