Showing posts with label java synchronization. Show all posts
Showing posts with label java synchronization. Show all posts

Wednesday, 25 September 2019

Java synchronization | what is java synchronization - online Help

Java synchronization:

Let's discuss what is java synchronization?

As we know, Java is a multithreaded program. so multiple threads can access the same data at a time. When multiple threads are accessing the single object. Then, there will be a data inconsistency. 

For example, 

Consider two people who have a joint account and the account balance is only 500 rupees. Both of them are accessing the account at the one time. One person wants to withdraw 200 rupees and another person wants to withdraw 400 rupees. 

Now the first person withdraws 200 rupees and the balance amount is only 300 rupees. The balance amount is not updated in the account.

Simultaneously the second person withdraws 400 rupees. Then the balance amount is 100 rupees, which is inconsistent and not correct.

To reduce this data inconsistency, we use thread synchronization. 

For example,

If we allow only one person to access the account at a time, the first person goes and withdraw 200 and the account will update the balance amount as 300 rupees.

Also read: Thread in java



Now if the second person accesses the account (try to withdraw 400 rupees) then it will show insufficient balance. because the balance amount is only 300 rupees.

Whenever multiple threads are trying to execute on a single object, we allow only one thread to be executed at a time is called Thread synchronization.



A thread can be synchronized by using the synchronized keyword as a modifier, applicable only for the method and blocks.

Synchronization cannot apply for the classes and variables.

When a thread begins executing a synchronized instance method, it automatically acquires a lock on those objects. The lock is automatically relinquished when the method completes.

General format for thread synchronization:

The general format is to synchronize access to common data is as follows:

synchronized(object)
{
// statement block
}

here,
the object is the name of the locking object

Sample program for Java Synchronization:

The following program illustrates the usage of synchronization. There are three threads each of which prints the string is shown below through a synchronized method,
good morning
good afternoon
good evening


When the thread is synchronized, the output should be:
good morning
good afternoon
good evening
good morning
good afternoon
good evening
good morning
good afternoon
good evening

When the threads are not synchronized, the output should be:
good morning
good morning
good morning
good afternoon
good afternoon
good afternoon
good evening
good evening
good evening

class ThreadX extends Thread
{
Thread s;
public void call(thread name)throws Exception
{
s=name;
s.start();
}
public void run()
{
try
{
caller(s);
}
catch(Exception obj)
{
}
}
synchronized void caller(Thread r) throws Exception
{
System.out.println("good morning");
System.out.println("good afternoon");
r.sleep(2000);
System.out.println("good evening");
}
}
class syndemo
{
public static void main(String args[])throws Exception
{
ThreadX one=new ThreadX();
Thread x=new Thread(one);
Thread y=new Thread(one);
Thread z=new Thread(one);
one.call(x);
one.call(y);
one.call(z);
}
}