answersLogoWhite

0

What is Thread Synchronization?

Updated: 8/9/2023
User Avatar

Wiki User

18y ago

Best Answer

Thread synchronization requires that a running thread gain a "lock" on an object before it can access it. The thread will wait in line for another thread that is using the method/data member to be done with it. This is very important to prevent the corruption of program data if multiple threads will be accessing the same data. If two threads try to change a variable or execute the same method at the same, this can cause serious and difficult to find problems. Thread synchronization helps prevent this.

User Avatar

Wiki User

18y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

13y ago

Why do we need Synchronization?

Threads are a powerful concept that we explained in the previous few chapters. Just as in the case of any programming scenario, the code works on a set of data and processes it. Lets imagine a scenario:

Assume that I have borrowed Rs. 10000/- from you and another Rs. 10000/- from an anonymous Mr. X last month. Now, both of you are chasing me for the money and I give you cheques worth the money I owe you guys, each worth Rs. 10000/- so, now I have given cheques worth Rs. 20000/- for payment. Unfortunately for you both, my account has a balance of only Rs. 12000/- and fortunately for me the banking application of my bank does not care about synchronization. So here is what will happen in a hypothetical situation wherein both the cheques are going to be submitted for payment at the same time.

Bank A (Your bank) and Bank B (Mr. X's bank) have both submitted the cheques for payment to my bank today morning at two different branches.

A teller is processing your cheque at branch 1 and another teller is processing Mr. X's cheque at branch 2. Both the tellers are simultaneously initiating the cheque clearance activity. Here is how it works:

Step 1: Teller 1 initiates a transfer at 10:30 AM and Teller 2 initiates a transfer at 10:30 AM as well

Step 2: Teller 1's system checks my bank balance at 10:31 AM and approves the payment because my account has Rs. 12,000 and Teller 2's system does the same at the same time and approves the payment because the money in my account is more than the value of the cheque I have given.

Step 3: Since both systems feel that there is enough balance in my account, they successfully make the payment.

2 cheques, each worth Rs. 10000/- got paid even though I had only Rs. 12000/- in my bank. Though I am happy about it, clearly the bank is not.

Guess what went wrong here?

There is no synchronization between the two tellers. Practically speaking, two tellers in two different branches don't need to talk to one another before processing a cheque but the banks application should handle such a situation.

All this happened because, the system allowed data related to one persons account to be accessed by two different processes(threads) and also allowed the data to be modified by both those processes at the same time.

This problem is known as a "race condition," where multiple threads can access the same Car (typically an object's instance variables), and can produce corrupted data if one thread "races in" too quickly before an operation that should be "atomic" has completed.

Practically speaking, only one of the cheques should have been cleared (whichever teller clicked "Transfer Funds" button first in milliseconds) and the other guys cheque must have bounced. This is exactly what would have happened if the first thread took a lock on the data and processed it, while all other threads that want access to the same data are in line "waiting". So, by the time the second teller clicked "Transfer funds" his process would have to wait until Teller 1's transfer is complete and then, the check balance logic would have come back stating "Hey buddy, this bum does not have enough cash to pay this cheque. Bounce this bad boy, will you?"

Now, I guess you know why we need synchronization (Only if you own a bank) but still, we now appreciate the need for synchronization.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is Thread Synchronization?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

How does thread synchronization occur in a monitor?

by using synchronized class


What is dfference between synchronization and asynchronization in java with example?

synchornisation is the process where more than one thread can be accessed by accesssing the shared object of the class.By syncornisation we can have the communication between threads.if one thread works then another thread automatically stops


Explain why interrupts are not appropriate for implementing synchronization?

Because they are completely unrelated things? Synchronization can be implemented with semaphores or mutexes.


Is an EUE thread a Non-Pressure sealed thread?

EUE thread is a pressure sealed thread as usual completion pup joints thread


Difference between yield method and sleep in java?

Thread.sleep(long milliseconds): The thread's sleep method sends the current thread into Non-Runnable state for the specified amount of time. But, this doesn't cause the thread to loose ownership of the acquired monitors. So, if the current thread is into a synchronized block/method, then no other thread will be able to enter that block/method.The Sleep method throws 'InterruptedException' if another thread interrupts it.There is another variant of the 'sleep()' method where it accepts two arguments - one, long milliseconds, and second, int nanoseconds. Simply put, this method causes the current thread to sleep for the specified number of milliseconds plus the specified number of nanoseconds. The second argument 'int nanoseconds' can acquire a value of the range 0-999999.wait() method: The wait() method also sends the current thread into Non-Runnable state like the sleep() method. But, the difference between the two is that in case of 'wait()' the locks are released before going into Non-Runnable state, so that any other thread that is waiting on the object can use it (Unlike the Sleep method - This is the big difference) Another apparent difference is that 'wait()' is an instance method, while sleep() is a static method. The method 'wait()' should be called for an object only when the current thread has already acquired lock for that object. This causes the current thread to wait either another thread invokes the 'notify()' method or the 'notifyAll()' method for this object, or a specified amount of time has elapsed and after that the thread starts participating in thread scheduling process to acquire the monitor of the object to proceed further.There are three variants of this method in the 'Object' class:-public final void wait(long timeout)public final void wait(long timeout, int nanoseconds)public final void wait()All the three methods throw InterruptedException & IllegalMonitorStateException. The first two may also throw IllegalArgumentException.The wait() method causes the current thread to place itself in the wait set for this object and then to relinquish any and all synchronization claims on this object. After the execution of this method invocation, the thread becomes disabled for any scheduling purposes and lies dormant until one of the following things happen:-* Any other thread invokes 'notify()' method this object and the thread under consideration is arbitrarily chosen as the thread to be awakened. * Any other thread invokes 'notifyAll()' for this object. * Any other thread interrupts the thread under consideration. * The specified amount of time has elapsed (in case first two variants of wait() are used) After any of the four above mentioned events happens, the thread is removed from the wait set for this object and re-enabled for thread scheduling. It'll compete for the rights to synchronize on the object in an usual manner and it'll keep doing this until it acquires control on the object and gains all the synchronization claims on the object, which it had acquired at the time of 'wait()' invocation. This means that after the return from wait() method, the synchronization state of object and of the thread will be exactly the same as it was before the wait() invocation.

Related questions

How does thread synchronization occur in a monitor?

by using synchronized class


What is thread synchonisation in os?

The thread synchronization is done for sharing of a common resource, to find the no. of resources in use, its count ,maximum resources in use... To establish this the various synchronization tools used are.. Events Waitable Timer


What is interprocess synchronization?

Processes might need to communicate to each. Interprocess of synchronization is the?æ management of resource among process. It ensures only a single thread (process) access a resource at a particular time.


What is dfference between synchronization and asynchronization in java with example?

synchornisation is the process where more than one thread can be accessed by accesssing the shared object of the class.By syncornisation we can have the communication between threads.if one thread works then another thread automatically stops


Synchronization in java?

Threads communicate primarily by sharing access to fields and the objects reference fields refer to. This form of communication is extremely efficient, but makes two kinds of errors possible: thread interference and memory consistency errors. The tool needed to prevent these errors is synchronization.


What is difference between synchronization and inter thread communication?

Synchronization is the process by which, access to the objects by threads are controlled and at at time only one thread can access an object. This is done to ensure that there are no dirty read/write operations. If multiple threads access the same data simultaneously, we may end up with inconsistent data. To avoid it, we place such code in synchronized blocks/methods to ensure integrity of data. Inter thread communication is the process by which java threads can communicate with one another. They communicate to one another through some commands like wait, notify, notifyall etc.


The provision of mechanisms for process synchronization?

The provision of mechanisms for process synchronization


When was Transactional Synchronization Extensions created?

Transactional Synchronization Extensions was created in 2012.


Why current is not concidered while synchronization?

current is load dependent,after synchronization only current will flow


What are examples of synchronization?

SONET


What do you mean by synchronization in operating system?

synchronization mean arrange all process in sequence for that not occured no colusion in the system.


Explain why interrupts are not appropriate for implementing synchronization?

Because they are completely unrelated things? Synchronization can be implemented with semaphores or mutexes.