What does 'synchronized' mean in JAVA ?

Posted By: Matpal Media - 11:13 PM
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.

0 comments:

Post a Comment

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