Java is a simple deadlock Program

Source: Internet
Author: User

 

Package namespace; Class dead implements runnable {public Boolean flag; public object Locka = new object (); Public object lockb = new object (); Public dead (Boolean f) {This. flag = f;} public void run () {While (true) {If (FLAG) {synchronized (lock. locka) {system. out. println ("If loca"); synchronized (lock. lockb) {system. out. println ("If lockb") ;}} else {synchronized (lock. lockb) {system. out. println ("else lockb"); synchronized (lock. locka) {system. out. println ("else Locka") ;}}}} class lock {static object Locka = new object (); static object lockb = new object ();} public class deadlock {public static void main (string ARGs []) {thread T1 = new thread (new dead (true )); thread t2 = new thread (new dead (false); t1.start (); t2.start () ;}}/** write a deadlock Program * The cause is that the two locks are not referenced in the same lock, which will lead to deadlocks. ** when there are many security problems in the current situation, consider the following: * 1. is there two threads, and there is no synchronization between the two threads, that is, there is no synchronized block or function * 2. if multiple syncs exist Code If the block or synchronous function is used, check whether all the synchronization functions use the same lock. If not, the deadlock and security problems may occur. ***/15:13:46

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.