Understanding from the ground up java-17.4 detailed explanation synchronization (1)-problems caused by competition conditions

Source: Internet
Author: User

Understanding from the ground up java-17.4 detailed explanation synchronization (1)-problems caused by competition conditions

In this chapter, we will discuss some synchronization topics and problems caused by competition conditions.

1. What are the competitive conditions?

When multiple threads or processes read and write a shared data, the result depends on the relative time of their execution. This situation is called competition.

The competition condition occurs when multiple processes or threads read and write data, and the final result depends on the command execution sequence of multiple processes.

For example:

We often encounter modifying a field in programming. This operation is especially prominent in the inventory. When two orders modify the inventory at the same time, a competition condition is formed, this is wrong because if the two orders are delivered at the same time, and the number of Outgoing orders is just greater than the inventory quantity, the problem may occur here. (Of course, there are several other problems. Here we just want to give an example of a competitive condition)

2. Let's take the example of bank transfers we mentioned earlier as an example to illustrate this problem.

We have established three categories:

Package com. ray. ch17; public class Bank {private final double [] accounts; public double [] getAccounts () {return accounts;} public Bank (int n, double initBalance) {accounts = new double [n]; for (int I = 0; I <accounts. length; I ++) {accounts [I] = initBalance ;}} public double getTotal () {double total = 0; for (int I = 0; I <accounts. length; I ++) {total + = accounts [I];} return total;} public void transfer (int fromAccount, int toAccount, double money) {if (accounts [fromAccount] <money) {return;} accounts [fromAccount]-= money; System. out. printf ("transferred from" + fromAccount + "account % 10.2f RMB,", money); accounts [toAccount] + = money; System. out. printf ("transferred from" + toAccount + "account to % 10.2f RMB,", money); System. out. printf ("Total: % 10.2f RMB", getTotal (); System. out. println ();} public int size () {return accounts. length ;}}
package com.ray.ch17;import java.util.Random;public class TransferThread implements Runnable {private Bank bank;private final double MAX;public TransferThread(Bank bank, double max) {this.bank = bank;this.MAX = max;}@Overridepublic void run() {while (true) {double amount = MAX * Math.random();int countOfAccount = bank.getAccounts().length;bank.transfer(new Random().nextInt(countOfAccount),new Random().nextInt(countOfAccount), amount);}}}
package com.ray.ch17;public class Test {public static void main(String[] args) {Bank bank = new Bank(100, 10000);for (int i = 0; i < 20; i++) {TransferThread transferThread = new TransferThread(bank, 10000);Thread thread = new Thread(transferThread);thread.start();}}}
Output:

Transfer RMB 8175.29 from 34 accounts and RMB 8175.29 from 39 accounts, total: RMB 1000000.00
Transfer RMB 165.66 from 16 accounts and RMB 165.66 from 88 accounts, total: RMB 1000000.00
Transfer RMB 3604.29 from 19 accounts and RMB 3604.29 from 72 accounts, total: RMB 1000000.00
Transfer RMB 3153.76 from 5 accounts and RMB 3153.76 from 41 accounts, total: RMB 1000000.00
Transfer RMB 1176.97 from 91 accounts and RMB 1176.97 from 32 accounts, total: RMB 1000000.00


......


Transfer RMB 8817.14 from 7 Accounts and RMB 8817.14 from 32 accounts, total: RMB 929031.75
Transfer RMB 2491.59 from 62 accounts and RMB 2491.59 from 90 accounts, total: RMB 927085.68
Transfer RMB 5800.54 from 45 accounts and RMB 5800.54 from 69 accounts, total: RMB 927085.68
Transfer RMB 1570.19 from 74 accounts and RMB 1570.19 from 17 accounts, total: RMB 926108.26
Transfer RMB 2550.74 from 83 accounts and RMB 2550.74 from 9 Accounts, total: RMB 926108.26
RMB 4207.99 was transferred from 3 accounts, and RMB 4207.99 was transferred from 73 accounts, total: RMB 924953.29
RMB 6369.35 was transferred from the 11 account, and RMB 6369.35 was transferred from the 38 account, total: RMB 924572.99

After running for a period of time, you will see that the total number is less, because of the above competition conditions and data synchronization is not performed.

Summary: This chapter describes the problems caused by competition conditions.

This chapter is here. Thank you.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.