| written 9.7 years ago by | • modified 9.7 years ago |
Mumbai University > Information Technology > Sem 3 > Object Oriented Programming Methodology
Marks: 5 M
Year: May 2014
| written 9.7 years ago by | • modified 9.7 years ago |
Mumbai University > Information Technology > Sem 3 > Object Oriented Programming Methodology
Marks: 5 M
Year: May 2014
| written 9.7 years ago by |
The keyword ‘synchronized’ and the lock forms the basis for implementing synchronized execution of code. There are two different ways in which execution of code can be synchronized:
1) synchronized methods
2) synchronized blocks
1) Synchronized Methods
The syntax of synchronized method is as follows:
class className
{
synchronized void methodName(parameters)
{
………….
…………. }
}
2) Synchronized blocks/statement
This is the general form of the synchronized statement:
synchronized(object) {
// statements to be synchronized
}
Here, object is a reference to the object being synchronized. A synchronized block ensures that a call to a method that is a member of object occurs only after the current thread has successfully entered object’s monitor.