Meet java-17.4 in the beginning (5)-Deadlock

Source: Internet
Author: User

In this chapter we will briefly discuss the deadlock, in fact, the deadlock is a relatively large topic, but I am here just according to the example of bank transfer in front of me, there is a detailed column to illustrate the various aspects of concurrent programming problems.

1. What is a deadlock?

Refers to two or more than two processes in the course of execution, because of competing resources or due to the communication between each other caused by a blocking phenomenon, if there is no external force, they will not be able to proceed.


2. How does it cause a deadlock?

(1) Mutually exclusive condition: A resource can only be used by one process at a time;

(2) Request and maintain conditions: holding the existing resources, but also to apply for other resources, but the application is not always blocked;

(3) Non-deprivation: the resources for the use of resources cannot be deprived by priority;

(4) Cyclic waiting condition: a cyclic waiting resource relationship is formed between several processes.


3. How did the previous example cause a deadlock?

(1) The process of deadlock generation:

Account 1 Original $200
Account 2 Original $300
Thread 1: From account 1 to 300 yuan to account 2
Thread 2: From account 2 to 400 yuan to account 1


(2) A block of code that causes a deadlock:

public void Transfer (int fromaccount, int. Toaccount, double money) {if (Accounts[fromaccount] < money) {System.out.prin TLN (Thread.CurrentThread (). GetName () + "deadlock appears"); 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 ();}

(3) Reason:

When the money transferred is larger than the existing balance, a deadlock will be generated


(4) Solution:

However, because the real business does not allow this situation to occur, so we directly return to solve the deadlock problem.


(5) If it is other multi-thread competing resources, it is not the same as the above return, and wait, there will be a deadlock situation.


4. Complete Example:

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) {System.out.prin TLN (Thread.CurrentThread (). GetName () + "deadlock"),//Thread.CurrentThread (). Wait ();//If it is another multi-threaded, it may be wait here, Thus a true deadlock return appears; 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 < 2; i++) {Transferthread transferthread = new Transferthread (bank, 10000); Thread thread = new Thread (transferthread); Thread.Start ();}}}


Summary: This section provides a brief introduction to deadlocks.


This chapter is here, thank you.

-----------------------------------

Directory



Meet java-17.4 in the beginning (5)-Deadlock

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.