Synchronization in java.

In this article,Synchronization in java is explained in detail with examples.
ITC stands for "Inter-thread communication".
When two or more threads are trying to access the same common data in a system then we use "ITC" mechanism,such common data is known as "Critical resonance/Data".
ITC(Inter Thread Communication)-javaform
In this case, there is a chance of getting critical resource/data corrupted i.e, it leads to inconsistent of data, to avoid this incorrectness of critical-resource-data in a multi-threading environment,we perform"Synchronization in Java".
Synchronization means proper communication between two or more threads trying to access the same data at the same time from different locations.(From topic Synchronization in java)
Since, In multi-tasking situations, others thread information is not known to each-others, JVM will take of.this situation and make sure that data is not corrupted or not incorrect.
Note
Hence, in the program, we use"Synchronized" keyword to the logic/method which leads to incorrectness to data.Doing so, JVM makes sure only 1 thread is given access to critical resource data at a single time and remaining threads are given proper messages i.e,"Blocked for IO state". 
(From topic Synchronization in java)
Ex
//program related to Synchronization in Java.
import java.io.*;
public class Deposit
{
static int balance=1000;
public static void main(String args[])
{
PrintWriter out=new
PrintWriter(System.out,true);
Account ac=new Account(out);
DepositThread first;
DepositThread second;
first=new DepositThread(ac,100,"#1");
second=new
DepositThread(ac,1000,"\t\t\t\t#2");
first.start();
second.start();
try
{
first.join();
second.join();
}
catch(InterruptedException e)
{
}
out.println("Final Balance is:"+balance);
}
}
class Account
{
PrintWriter out;
public Account(PrintWriter out)
{
this.out=out;
}
public synchronized void deposit(int amount,String name)
{
int balance;
out.println(name+" trying to deposit: "+amount);
out.println(name+" getting balance...");
balance=getBalance();
out.println(name+" balance got is: "+balance);
balance+=amount;
out.println(name+" setting balance... ");
setBalance(balance);
out.println(name+" new balance is set to: "+Deposit.balance);
}
public int getBalance()
{
try
{
Thread.sleep(5000);
}
catch(InterruptedException e)
{
}
return Deposit.balance;
}
public void setBalance(int balance)
{
try
{
Thread.sleep(5000);
}
catch(InterruptedException e)
{
}
Deposit.balance=balance;
}
}
class DepositThread extends Thread
{
Account account;
int amount;
String message;
public DepositThread(Account account,int amount,String message)
{
this.account=account;
this.amount=amount;
this.message=message;
}
public void run()
{
account.deposit(amount,message);
}
}
Output
Synchronization-output-javaform
                           To get the code file use me.
Note
In Multi-threading Environment,where a group of threads are doing their job on same critical resource data and few threads finishes their job early,such threads in a group can be called with join() method.
JVM make sure,these Threads are not immediately gone to stop state but goes to the sleep state and after all remaining threads finish their respective jobs then all the threads in the group are stopped at a time and dead state for garbage collection.(From topic Synchronization in java)


Continue to the next topic Abstract class and Abstract methods.


Begin your career in Digital Marketing,What is digital marketing? Digital Marketing online course. It's an current evolving technology which can give support to establish your own startup through Digital Marketing 


Do check my new startup Surprise Planners in HyderabadLavish Surprises our services are surprise party planners in Hyderabadsurprise giftssurprise birthday party planners Hyderabad, Wedding anniversary surprisessurprise planners Hyderabad.

Hi Friends, Please comment down your views in the comment section below. Thank you...

No comments:

Connect with Us by Subscribing here for more Updates..!

* indicates required