Encryption in java!

Source: Internet
Author: User

1. About deadlock

Deadlock, we can simply understand that two threads use the same resource at the same time, and the two threads do not get the corresponding resources, resulting in no waiting for each other.

2. simulate a deadlock

Background: we create a friend class. When a friend bows to us, we also bow to our friends. This is a complete action. When two

While bowing, waiting for the other party to bow. A deadlock occurs.

Simulation Program:

Package com. yxy. thread;

/**

* @ Author windows

* Deadlock simulation program

*/

Public class Deadlock {

/**

* @ Author windows

* Friend entity class

*/

Static class Friend {

// Friend name

Private final String name;

// Construct a friend object type

Public Friend (String name ){

This. name = name;

}

// Obtain the name

Public String getName (){

Return this. name;

}

// A friend bowed his way to me (synchronous)

Public synchronized void bow (Friend bower ){

System. out. format ("% s: % s"

+ "Has bowed to me! % N ",

This. name, bower. getName ());

Bower. bowBack (this );

}

// I bow back (synchronous)

Public synchronized void bowBack (Friend bower ){

System. out. format ("% s: % s"

+ "Has bowed back to me! % N ",

This. name, bower. getName ());

}

}

Public static void main (String [] args ){

// Test the deadlock simulation program

// Create two friends, alphonse and Gaston

Final Friend alphonse =

New Friend ("Alphonse ");

Final Friend gaston =

New Friend ("Gaston ");

// Start the thread of two friends.

New Thread (new Runnable (){

Public void run () {alphonse. bow (gaston );}

}). Start ();

New Thread (new Runnable (){

Public void run () {gaston. bow (alphonse );}

}). Start ();

}

}

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.