In this chapter we discuss some of the topics of synchronization, which are caused by competitive conditions.
1. What are the competitive conditions?
When multiple threads or processes read and write a shared data, the results depend on the relative time they execute, a situation called competition.
The competition condition occurs when multiple processes or threads are reading and writing data, and their final result depends on the order of instruction execution of multiple processes.
To give an example:
Our usual programming often encountered the modification of a field, this operation in the inventory is particularly prominent, when the two list at the same time to modify the inventory, then the formation of a competitive condition, if not to do synchronous processing, here is a mistake, because if the two list at the same time out of the library, and the number of out of the library is just larger There's going to be a problem here. (Of course, there are a few things that are going to happen, and we're just trying to give an example of a competitive condition.)
2. Here is an example of a bank transfer example we mentioned earlier to illustrate this issue.
We build 3 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 ("from" + Fromaccount + "account transfer%10.2f Yuan,", money); Accounts[toaccount] + = money; System.out.printf ("from" + Toaccount + "account transferred to%10.2f Yuan,", money); System.out.printf ("Total:%10.2f yuan", 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 (). Leng Th;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 (+ 10000); for (int i = 0; I < 20; i++) {Transferthread transferthread = new Transferthread (bank, 10000); Thread thread = new Thread (transferthread); Thread.Start ();}}}
Output:
Transfer $8175.29 from 34 account, transfer from 39 to 8175.29 yuan, total: 1000000.00 yuan
Transfer $165.66 from 16 account, transfer from 88 to 165.66 Yuan, total: 1000000.00 yuan
Transfer $3604.29 from 19 account, transfer from 72 to 3604.29 yuan, total: 1000000.00 yuan
Transfer $3153.76 from 5 account, transfer from 41 to 3153.76 yuan, total: 1000000.00 yuan
Transfer $1176.97 from 91 account, transfer from 32 to 1176.97 yuan, total: 1000000.00 yuan
......
Transfer $8817.14 from 7 account, transfer from 32 to 8817.14 yuan, total: 929031.75 yuan
Transfer $2491.59 from 62 account, transfer from 90 to 2491.59 yuan, total: 927085.68 yuan
Transfer $5800.54 from 45 account, transfer from 69 to 5800.54 yuan, total: 927085.68 yuan
Transfer $1570.19 from 74 account, transfer from 17 to 1570.19 yuan, total: 926108.26 yuan
Transfer $2550.74 from 83 account, transfer from 9 to 2550.74 yuan, total: 926108.26 yuan
Transfer $4207.99 from 3 account, transfer from 73 to 4207.99 yuan, total: 924953.29 yuan
Transfer $6369.35 from 11 account, transfer from 38 to 6369.35 yuan, total: 924572.99 yuan
After running for a period of time you will see that the total is less, this is because of the competition conditions above, and did not do data synchronization formed.
Summary: This chapter focuses on issues raised by competitive conditions.
This chapter is here, thank you.
-----------------------------------
Directory
Understanding java-17.4 in the beginning (1)-problems caused by competitive conditions