Analysis of multithreading concurrent processing instance in Java programming
This paper mainly demonstrates the multithreading concurrent processing scenario of Java programming through an example of bank users withdrawing money, as follows.
Start with an example: implement the example code of a bank account withdrawal scenario.
The first class: account java
Account type:
The second class: drawthread java
Withdrawal thread class:
The parts commented out in the above code: (1) synchronized synchronization code block (2) thread sleep. If (1) and (2) are commented out, there are many possibilities for the running result, one of which (the probability is small) is in line with the normal logic:
B withdraws money successfully, spits out notes: 800.0, the balance is: 200.0 a withdraws money failed, the balance is insufficient!
B should first find the withdrawal resource and correct the balance before a starts to judge the user's balance; This probability is very small, and most operations will be similar to the following:
A withdraws money successfully, spits out notes: 800.0 B withdraws money successfully, spits out notes: 800.0, balance: - 600.0, balance: 200.0
This is obviously illogical. It can be guessed from the operation results that a preempts the resources and takes out the amount, but the resources are preempted by B before modifying the balance; Since the balance has not been modified, B sees that the balance is still 800, and B still withdraws the amount; A first runs to modify the balance but does not print it, and B grabs resources; B modify the balance and print the balance as - 600; A print balance, 200;
If (2) thread sleep is added, it must be an error, because a or B will release CPU resources because of sleep after withdrawing the amount, and the JVM will call other processes in preparation. The second withdrawal judgment balance must be wrong.
If (1) synchronized synchronization code block is added to lock the account in the thread run method body, the execution logic will be guaranteed to be normal every time:
A withdraws money successfully, spits out notes: 800.0, the balance is: 200.0, B withdraws money failed, the balance is insufficient! You can imagine the execution process:
A preempts resources first and locks the account class initially in the run method body; Then start executing the synchronization code block; If an intermediate link is executed, CPU resources are preempted by B; B starts to execute and locks the account class at the beginning. However, when locking, it will be found that the account has been occupied by a, then it will be adjusted to the blocking state and wait for a to release resources; A releases the account lock after executing the synchronization code block, and B continues to execute; The balance guarantee seen by B during operation is that a has been modified and will be executed normally according to the correct logic.
summary
The above is all about the analysis of multithreaded concurrent processing examples in Java programming. I hope it will be helpful to you. Interested friends can continue to refer to other related topics on this site. If there are deficiencies, please leave a message to point out. Thank you for your support!