Simple example of synchronous nesting of JavaSE8 Foundation deadlocks

Source: Internet
Author: User

Li Wu:
Learn to think more, honouring teachers save Thanksgiving. Leaf See root Three return one, living water Qingyuan hidden in zero.
Meekness The Lord of Conscience, Haoyuedangkong the King of Freedom. Willing to give the most ascetic no regrets, sincere advice and the line and cherish.



Os:windows7 x64
Jdk:jdk-8u131-windows-x64
Ide:eclipse Oxygen Release (4.7.0)



Subclass that inherits the thread:

Package blog.jizuiku1;/* * @author Jizuiku * @version V17.09.27 */public class Dielock extends Thread {//because it is a synchronous nested demo deadlock, the Create two locks with the need for private static object Locka = new Object ();p rivate static Object lockb = new Object ();//Create a tag, it will be a coincidence with If-else Wonderful effect Private Boolean Flag;public Dielock (Boolean flag) {super (); this.flag = Flag;} @Overridepublic void Run () {//TODO auto-generated method Stubwhile (True) {//If a thread's flag is true, it grabs the execution right after it will lock the Locka//At the same time, another The flag of a thread is false, it grabs the execution right, it will lock the LOCKB//so that the first thread can only go down when the lockb is unlocked/The awkward thing is that the second thread can only go down when the locka is unlocked/So, These two threads start to wait long ...//Get down if you don't have it ...... If you persist, your life will pass. if (flag) {synchronized (Locka) {///thread wants to come here, it needs locka to be untied//when the thread comes here, Locka is locked System.out.println (" Flag=true,locka "); synchronized (lockb) {System.out.println (" flag=true,lockb ");}}} else {synchronized (LOCKB) {//thread want to come here, then need to lockb is untied//when the thread comes here, lockb is locked System.out.println ("flag=flase,lockb"); Synchronized (Locka) {System.out.println ("Flag=flase,locka");}}}}}


Test class:

Package blog.jizuiku1;/* * @author Jizuiku  * @version V17.09.27 */public class Dielockdemo {public static void main (St Ring[] args) {Dielock dl1 = new Dielock (true);D ielock DL2 = new Dielock (false);d L1.start ();d L2.start ();}}


Results:


Feelings:
Saw this scene, to the most bitter can not help but think of two pictures

Java is good, worth learning.
Learning resources: Itcast and Itheima Video library. If you have public resources, can share to me, with your resources to learn also can.
Blog post is to watch the video, into thinking written. It's good that the teacher speaks well. Blog bad, is to give the most bitter not serious.

Simple example of synchronous nesting of JavaSE8 Foundation deadlocks

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.