What is synchronized in java?
Synchronized::
--> It is the modifier applicable only for methods and blocks but not for classes and variables
--> If multiple threads are trying to operate simultaneously on the same java object then there may be chance of data inconsistency problem.To overcome this problem we should go for synchronized keyword.
--> If a method or block declared as synchronized then at a time only 1 thread is allowed to execute that method or block on given object.so that data inconsistency problem will be resolved.
--> The main advantage of synchronized keyword is we can resolve data inconsistency problems but the main disadvantage of synchronized keyword is it increases waiting time of threads and creates performance problems.Hence if there is no specific requirement then it is not recommended to use synchronized keyword.
--> Internally synchronization concept implemented by using lock.Every object in java has a unique lock.
--> whenever we are using synchronized keyword then only lock concept will come into picture.
**When a method is shared by 2 threads, then 2 threads executes in asynchronous manner.Here No order is maintained is maintained for threads.To solve the problem we use the special keyword synchronized.
Without using Synchronized |
Demo on program without using Synchronized:
public class synchronizationDemo implements Runnable {
public void run() {
for (int i = 1; i <= 6; i++) {
System.out.println("without synchronized method" + Thread.currentThread());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
synchronizationDemo sd = new synchronizationDemo();
Thread td1 = new Thread(sd);
Thread td2 = new Thread(sd);
td1.start();
td2.start();
}
}
Output:
without synchronized methodThread[Thread-0,5,main]
without synchronized methodThread[Thread-1,5,main]
without synchronized methodThread[Thread-1,5,main]
without synchronized methodThread[Thread-0,5,main]
without synchronized methodThread[Thread-1,5,main]
without synchronized methodThread[Thread-0,5,main]
without synchronized methodThread[Thread-0,5,main]
without synchronized methodThread[Thread-1,5,main]
without synchronized methodThread[Thread-1,5,main]
without synchronized methodThread[Thread-0,5,main]
without synchronized methodThread[Thread-0,5,main]
without synchronized methodThread[Thread-1,5,main]
Here, observe above program we have not used Synchronized keyword.And we have 2 threads td1 and td2.So these 2 threads can
access method run() simultaneously and if we execute the program we will get mixed outputs.
With synchronized
Demo on program using Synchronized:
public class synchronizationDemo implements Runnable {
synchronized public void run() {
for (int i = 1; i <= 6; i++) {
System.out.println("with synchronized method" + Thread.currentThread());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
synchronizationDemo sd = new synchronizationDemo();
Thread td1 = new Thread(sd);
Thread td2 = new Thread(sd);
td1.start();
td2.start();
}
}
Output:
with synchronized methodThread[Thread-0,5,main]
with synchronized methodThread[Thread-0,5,main]
with synchronized methodThread[Thread-0,5,main]
with synchronized methodThread[Thread-0,5,main]
with synchronized methodThread[Thread-0,5,main]
with synchronized methodThread[Thread-0,5,main]
with synchronized methodThread[Thread-1,5,main]
with synchronized methodThread[Thread-1,5,main]
with synchronized methodThread[Thread-1,5,main]
with synchronized methodThread[Thread-1,5,main]
with synchronized methodThread[Thread-1,5,main]
with synchronized methodThread[Thread-1,5,main]
Here, observe above program we have used Synchronized keyword.And we have 2 threads td1 and td2.So these 2 threads can access
method run() in an order i.e., thread td2 will be in waiting state until thread td1 completes its task.If we execute the program we will
get constant outputs.
Please comment below to feedback or ask questions.
No comments:
Post a Comment
Please comment below to feedback or ask questions.