synchronization in java thread synchronization in java synchronization in java example synchronization in java with example thread synchronization in java example synchronization of threads in java synchronization block in java synchronization example in java thread synchronization example in java example of synchronization in java threads synchronization in java synchronization in java example programs synchronization in java example code
Synchronization:
The basic aim of
synchronization concept is to get consistent result by eliminating
inconsistent
results. When multiple threads are collectively working on or entering in
common or sharable area then those multiple threads generates inconsistent
results.
To avoid this inconsistent result we must use concept of
synchronization.
Definition Of
Synchronization:
The process of
allowing only one thread among multiple threads into the area which is
sharable
to perform read and write operations is known as synchronization. What are all the
concurrency inconsistent results are generated, which can be eliminated by the
concept of synchronization. Synchronization always applicable on dependent
operations.
Need Of
Synchronization:
Let us assume there
exists a shareable variable ‘balance’ whose initial value is 0.
Let us assume
their exist two threads (say t1 and t2) and these threads want to deposit
Rs 10
and 20/- in the sharable variable ‘balance’ respectively. Both the threads are
started
their execution with the milliseconds of time and deposited Rs 10 and
20/- in the variable
‘balance’ then the final value of variable ‘balance’ is
either if 20 but not 30. Which is one of the
inconsistent result. To avoid this
inconsistent result we must apply the concept of
synchronization.
Synchronization
Concept is Applied:
Let us assume both
the threads started their execution (t1 and t2) with the difference
of
milliseconds of time (t1 is first later t2) for depositing Rs 10,20/- in the
sharable variable
‘balance’. At this point of time the value of balance
variable is locked by JVM. During this
period of time if thread t2 is trying to
access balance variable value then the thread t2 is
trying to access balance
variable value then the thread t2 will be made wait until t1 completes
its
execution. Once thread t1 completes its execution, the balance variable will be
unlocked
and given the value of balance variable (10) to thread t2 and once
again balance variable will
be locked by JVM. Thread t2 also completes its
execution, balance variable will be unlocked
and the final value of balance
variable will locked by JVM. Thread t2 also completes its
execution, balance
variable will be unlocked and the final value of balance variable is 30 which
is one of the inconsistent result.
Hence during
synchronization process the concept of locking unlocking will be continued
by
JVM until all the threads are completed their execution.
Thread Synchronization Techniques:
These are
classified into two types. They are
1)
Synchronized methods
2)
Synchronized Blocks.
1) Synchronized Methods:
If
ordinary methods are accessed by multiple threads concurrently then in the
ordinary
method multiple threads generates inconsistent results.
To avoid this inconsistent results, the ordinary methods definition must
be made as
Synchronize by using Synchronized keyword.
Based on writing the Synchronized
keyword before the methods definition, methods are
classified into two types.
a)
Synchronized instance methods.
b)
Synchronized static methods.
a) Synchronized instance methods:
If an ordinary instance method is accessed by multiple threads
concurrently
then ordinary instance methods definition generates inconsistent
results.
To avoid this inconsistent results
the definition of ordinary instance method must be preceded by a keyword synchronized.
Syntax:
Synchronized Return-Type Method-Name(List-of-foraml-parameters-if-any)
{
------//Block of Statements
}
Point
To Remember :
Once an ordinary instance methods is synchronized then corresponding
class
object will be locked by JVM.
For Example:
Class
Account
{
Private int
bal=0;
Synchronized
void deposit(int amount)
{
balance=balance+amount;
System.out.println(“current balance =”+balance);
}
}
As long as one thread is executing the above Synchronized
instance deposit() then
object of Account class will locked by JVM.
b) Synchronized Static method:
When an ordinary static
method accessed by multiple threads concurrently then ordinary static method
generates inconsistent results.
To avoid these inconsistent results, the definition of ordinary static
methods must be preceded by a key word Synchronized.
Syntax:
Synchronized static Return-Type
Method-Name(List-Of-Formal-Parameters-if-any)
{
-------//Block Of Statements
}
Eg:
Class Account
{
Public int bal=0;
Synchronized static
deposit(int amount)
{
bal=bal+amount;
System.out.println(“Current
balance :”+bal);
}
}
As long as one thread is executing the above synchronized static
deposit() then JVM will lock the corresponding class call Account.
2) Synchronized Blocks:
Synchronized block concept is an
alternative technique of synchronized methods for getting the consistent
result.
Synchronized blocks must be always
written inside the normal instance methods / inherited non- synchronized instance
methods but not in static methods. Because it is always recommended to override
instance method but not static methods.
Since synchronized blocks we
writing in non-synchronized instance methods,JVM always locks object of current
class.
Syntax:
Sychronized(object of current class)
{
//Block of statements
}
For Example:
Class SAccount
{
Public void deposit(int amount)
{
Sychronized(this)
{
bal=bal+amount;
System.out.println(“Current
balance:”+bal);
}
}
}
No comments:
Post a Comment