answersLogoWhite

0

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

19y ago

Still curious? Ask our experts.

Chat with our AI personalities

LaoLao
The path is yours to walk; I am only here to hold up a mirror.
Chat with Lao
BlakeBlake
As your older brother, I've been where you are—maybe not exactly, but close enough.
Chat with Blake
RossRoss
Every question is just a happy little opportunity.
Chat with Ross
More answers

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.

User Avatar

Wiki User

14y ago
User Avatar

Add your answer:

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