This is similar to my previous question as I am still unclear with synchronized keyword.
This time I'll make it very short .
private int count = 0;
synchronized void increment() {
count++;
}
count is an instance variable which is shared among 2 threads.
If Thread t1 and t2 tries to increment count and OS gives t1 a chance to increment count first :-
t1 takes the lock and atomically increments count, time taken to increment count is 1 min(consider) including time taken to get the lock .
But what about thread t2, It is has to wait until lock is released. After release of lock, t2 now increments count atomically which also takes 1 min .
So synchronization gives correctness but it also takes time to perform.Threads are meant for doing work in less amount of time so why to use synchronization in threads what's the use of it.
Is my understanding correct ?