Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (1.1k points)

I have some questions regarding the usage and significance of the synchronized keyword.

  • What is the significance of the synchronized keyword?
  • When should methods be synchronized?
  • What does it mean programmatically and logically?

1 Answer

0 votes
by (13.2k points)

When a resource is being shared by more than one thread, synchronization is needed which can be done by the keyword synchronized in java.

Take for example, a thread made a change to the shared data and another thread wants to now access this data, the data that his resource will get should contain all the changes made by the previous thread.To avoid anonymity and confusion coordination of events it is needed so that the threads are in unison.

// Only one thread can execute at a time. 

// sync_object is a reference to an object

// whose lock associates with the monitor

// The code is said to be synchronized on

// the monitor object

synchronized(sync_object)

{

   // Access shared variables and other

   // shared resources

}

Related questions

Browse Categories

...