Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...

How does synchronized modifier work?

Answer Posted / javamasque

Synchronized is used to make a resource thread safe. If an object or resources is accessed by more than one thread, then these should be under synchronization. Only method and block can be synchronized.

Synchronized method
• As an instance method is synchronized then, the object is locked for a period of time as a thread is accessing the method.
• As a class method (static) is synchronized then, whole class is locked for a period of time as a thread is accessing the class method.

Synchronized block
• A particular set of statements (lines of codes) inside method or block is synchronized with synchronized block.
• An object reference is passed inside synchronized block to make a particular set of statements inside instance method or block.

synchronized (this){
}
• If a particular set of synchronized statements inside instance method or block is accessed by a thread then whole object is locked.
• A class reference is passed inside synchronized block to make a particular set of statements inside static method or block.

Synchronized (Class.class){
}

Is This Answer Correct ?    4 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Define how does a try statement determine which catch clause should be used to handle an exception?

1088


Give an example of use of pointers in java class.

981


What are advantages of exception handling in java?

985


What is the primary benefit of encapsulation?

1023


What language is java written?

976


What is a function easy definition?

969


Can a constructor have different name than a class name in java?

1104


How many types of memory areas are allocated by jvm?

991


how can i use a nonsynchronized hashtable?

2539


What is nullpointerexception in java?

1060


What is the exact difference in between Unicast and Multicast object?

2063


Do you need to import math in java?

952


Given a singly linked list, determine whether it contains a loop or not without using temporary space?

944


How many bits is a string in java?

983


In which language java is written?

936