WHAT IS SYNCHRONIZED METHOD ?

The synchronized keyword indicates that a method can be accessed by only one
thread at a time.  A typical synchronized
declaration looks like this:
public synchronized Record retrieveUserInfo(int id) { }

the synchronized modifier can be matched with any
of the four access control levels (which means it can be paired with any of the three
access modifier keywords).
in otherwords the method is thread safe. That means only one thread can access at a time.

2 comments:

  1. what is the advantage of synchronized method over the normal method ?

    ReplyDelete
    Replies
    1. I think if we will give normal method in place of Synchronized method than, at a time many thread object will execute that method so the out put will be simultaneously , might be a chance of data corruption . To avoid data corruption we use Synchronized method

      Delete