Multi-threaded deadlock (Dead Lock) issues in C #

Source: Internet
Author: User

See an example of life before you talk about multi-threaded deadlocks, which makes it easier to immediately deadlock in C #.

Xiao Ming and Xiao Hong is an electronic information professional class classmate, they in senior year when the election is the same tutor's same topic as a graduation thesis. In the process of writing a paper to inquire "analog electronic Technology (book)" and "analog electronic technology (next book)" These two materials, these two books can be borrowed in the book edition. Now Xiao Ming borrowed "the book", and Little Red borrowed "book", and then they began to write their own papers. One weeks later, Xiao Ming found that he also needed to review the "book" and Cooperate with "book" in order to continue writing the paper. But at the same time Little red but found that she needs "the book" Information combined with the hands of the "book" in order to continue to write. At this time, Xiao Ming found Little Red said: "I need the book", and so I finished writing the paper, I will give you all the books. "But little red is also very anxious, because the graduation reply is imminent, she also urgently needs two books simultaneously reference can complete the thesis." Little Red said: "Give me The book", and so I finish writing the paper, I will give you the book. ”

At this time, Xiao Ming and Xiao Red, Xiao Ming occupies the "book" will not let go, at the same time he also need "book." And Little Red occupies the "book" will not let go, at the same time she also need to "book." This is the "deadlock" in real life, because if they are unwilling to give up their possession of the book, then everyone can not continue their own affairs.

Deadlocks in C # are roughly similar to the previous example. A thread 1 occupies resource A, and it requires resource B, while a thread 2 occupies resource B, which requires resource a. Two threads are waiting for each other to release resources to get the other's resources for subsequent work. In this case, a deadlock is caused. The following code shows the case of small red and xiaoming in the previous example:

        Static stringBooka ="Analog electronic Technology (in the book)"; Static stringBOOKB ="Analog Electronic Technology (next book)"; Static ObjectLockobj =New Object(); Static voidMain (string[] args) {Console.WriteLine ("Xiao Ming and Xiao Hong is an electronic information professional class classmate, they in senior year when the election is the same tutor's same topic as a graduation thesis. In the process of writing the paper, we have to inquire "analog electronic Technology (book)" and "Analog Electronic technology (book)" The two materials, these two books can be borrowed in the book version. "); Thread wokerxiaoming=NewThread (Xiaoming_writepaper); Thread Wokerxiaohong=NewThread (Xiaohong_writepaper);            Wokerxiaoming.start ();            Wokerxiaohong.start ();            Wokerxiaoming.join ();            Wokerxiaohong.join (); Console.WriteLine ("Xiao Hong and Xiao Ming finished the paper, successfully graduated!"); }        Static voidXiaoming_writepaper () {Console.WriteLine ("Xiao Ming began to write his thesis."); Lock(Booka) {Console.WriteLine ("Xiao Ming got it:"+Booka); Thread.Sleep (Timespan.fromseconds (2)); Console.WriteLine ("at this time, xiaoming need: Analog electronic technology (next book), he is waiting for Little Red to "book" after reading to him. "); Lock(BOOKB) {Console.WriteLine ("Xiaoming got it."+BOOKB); Thread.Sleep (Timespan.fromseconds (1)); Console.WriteLine ("Xiao Ming has finished his thesis."); }            }        }        Static voidXiaohong_writepaper () {Console.WriteLine ("Xiao Hong began to write papers."); Lock(BOOKB) {Console.WriteLine ("Little Red got it:"+BOOKB); Thread.Sleep (Timespan.fromseconds (2)); Console.WriteLine ("at this point, Little red needs: analog electronic technology (book), she is waiting for Xiao Ming to read the "book" after reading to her. "); Lock(Booka) {Console.WriteLine ("Little Red got it."+Booka); Thread.Sleep (Timespan.fromseconds (1)); Console.WriteLine ("Xiao Hong has finished writing the paper."); }            }        }

Operation Result:

Xiao Ming and Xiao Hong is an electronic information professional class classmate, they in senior year when the election is the same tutor's same topic as a graduation thesis. In the process of writing a paper to inquire "analog electronic Technology (book)" and "analog electronic technology (next book)" These two materials, these two books can be borrowed in the book edition. Xiao Ming began to write his thesis. Xiao Ming got it: analog electronic Technology (book) Little Red began to write a paper. Little Red Got it: analog electronics Technology (next book) at this time xiaoming need: Analog electronic technology (next book), he is waiting for Little Red to read "book" to him after reading. At this point, Little red needs: analog electronic technology (book), she is waiting for Xiao Ming to read the "book" after reading to her.

Multi-threaded deadlock (Dead lock) problem in C #

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.