Java Thread Synchronization:
When synchronization is required?
synchronization is reqired when two threads are accessing same resource if you want to protect that resource the synchronization is required.
How many ways we can achieve synchronization ?
there is only two ways we can achieve 1.method level2.block level
how to achieve synchronization ?
using synchronization key word in javasynchronized keyword was only way to provide synchronization of shared object in Java. Any code written by using synchronized block or enclosed inside synchronized method will be mutually exclusive, and can only be executed by one thread at a time. You can have both static synchronized method and non static synchronized method and synchronized blocks in Java
How synchronization worked in java?
synchronization works on based on object lock mechanism.every object in java has a built in lock that only comes to play when that object has synchronization method codewhen thread enters a synchronized non-static method,
we automatically acquire the lock associated with current instance of the class there is only one lock for object the one thread pick the lock means no other thread can't pick the lock until current thread release the lock.
using blocks with in method you can restrict some code only for synchronization remaining code in the method can accessible for other thread using this block performance will be goodwhy because only piece code in the method only synchronized.
Difference between static synchronization and non-static synchronization ?
static synchronization is class level lockall instance of that particular class have only one lock
example:Student s1=new Student();
Student s2=new Student();both s1 and s2 have only one lock in class level lock in non-synchronization in case of non-static synchronization both s1 and s2 have different locksthat mean one thread only need to modify s1 object it not allow any other threadbut two threads can change both s1 and s2 parallel in nonstatic-synchronization
the static method lock on Class instance while non-static method lock on this instance
*only method or block can be synchronized*each object has only one lock*a class can have both synchronization and non-synchronization
Consider one example raju and rani have one join account account balance is 50.and they are accessing same time what will happen?
see this example
package com.multi;
public class ProblemWithTh implements Runnable {
Account account=new Account();
public static void main(String[] args) {
ProblemWithTh problemWithTh=new ProblemWithTh();
Thread thread1=new Thread(problemWithTh);
Thread thread2=new Thread(problemWithTh); thread1.setName("raju");
thread2.setName("rani");
thread1.start();
thread2.start();
}
@Override public void run()
{
for(int i=0;i<=5;i++){
makeWithDraw(10);
if(account.getBalance()<0){ System.out.println("overdrwannnn");
}
}
}
private void makeWithDraw(int i) {
if(account.getBalance()>=i){
System.out.println(Thread.currentThread().getName()+"::::is going to with draw current balance is"+account.getBalance()); account.withdrawl(i);
System.out.println("::after "+Thread.currentThread().getName()+"::with drawl account balance:::::::"+account.getBalance());
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block e.printStackTrace(); }
}
else{ System.out.println("no nougat money for"+Thread.currentThread().getName());
}
}
}
Output:
raju::::is going to with draw current balance is50
::after raju::with drawl account balance:::::::40
rani::::is going to with draw current balance is50
::after rani::with drawl account balance:::::::30
raju::::is going to with draw current balance is30::
after raju::with drawl account balance:::::::20
rani::::is going to with draw current balance is30
::after rani::with drawl account balance:::::::10
rani::::is going to with draw current balance is10
::after rani::with drawl account balance:::::::0
raju::::is going to with draw current balance is10
::after raju::with drawl account balance:::::::-10
overdrawn
overdrawn
no enough money for rajuoverdrawnno enough money for ranioverdrawnno enough money for rajuoverdrawnno enough money for ranioverdrawnno enough money for ranioverdrawnno enoug money for rajuoverdrawn
raju and rani using one account if one preforming withdrawal or any other operation another thread need to wait in above example
i did n't use any synchronization that why in output after two threads are accessing simultaneously.
because of this reason after with drawl of raju for rani it displaying wrong amountrani will shock before withdrawal account balance is 50 after withdrawal 10 balance is 30 :)
make sure that output you will not be same all time two are different. based on thread scheduler selection threads will get chance that's why out put will not be same all time.
just i added synchronized word on which method i need protect
by accessing multiple threads.
private synchronized void makeWithdraw(int i) {
if(account.getBalance()>=i){
System.out.println(Thread.currentThread().getName()+"::::is going to with draw current balance is"+account.getBalance()); account.withdrawl(i);
System.out.println("::after "+Thread.currentThread().getName()+"::with drawl account balance:::::::"+account.getBalance()); try { Thread.sleep(500);
}
catch (InterruptedException e) {
// TODO Auto-generated catch block e.printStackTrace(); }
}
else{ System.out.println("no enough money for"+Thread.currentThread().getName());
}
}
output:rani::::is going to with draw current balance is50
::after rani::with drawl account balance:::::::40
rani::::is going to with draw current balance is40
::after rani::with drawl account balance:::::::30
rani::::is going to with draw current balance is30
::after rani::with drawl account balance:::::::20
raju::::is going to with draw current balance is20::
after raju::with drawl account balance:::::::10
raju::::is going to with draw current balance is10::
after raju::with drawl account balance:::::::0
no enough money for rajuno enough money for rani
no enough money for rani
no enough money for rani
no enough money for raju
no enough money for raju
no enough money for raju
problem is solved using synchronized key word it allow only one thread instance at a time
while rani withdrawal raju will wait raju withdraw rani will wait :)
As the post said online education is best ,so get your best academic education here
ReplyDeletehttp://www.kidsfront.com/
i myself recommend my tution students
Revit Architecture Training in Delhi
ReplyDelete