Java --- 16 --- multithreading --- deadlock

Source: Internet
Author: User

Java --- 16 --- multithreading --- deadlock

 

Deadlock:

Concept:

Deadlock: a deadlock occurs when two or more processes compete for resources during execution. If there is no external force, they will not be able to proceed. It is said that the system is in a deadlock state or the system has a deadlock. These processes that are always waiting for each other are called deadlock processes. Because resource usage is mutually exclusive, after a process applies for resources, the relevant process will never be allocated with necessary resources and cannot continue to run without external assistance, this produces a special phenomenon: deadlock.

 

 

 

Conditions for deadlock:

1. mutex condition: A resource can only be used by one thread at a time.

2. Non-preemption condition (non-deprivation condition): the resources obtained by the current process lock cannot be forcibly deprived before it ends.

3. Possession and application conditions (request and maintenance conditions): a process already has certain resources and wants to apply for other resources, but does not give up on its own resources.

4. Cyclic condition: the relationship between several processes that are connected by the beginning, end, and end of a loop waiting for resources


 

 

When does it usually appear? Nested synchronization in synchronization

Create a deadlock:


 

class Test2 implements Runnable{    private boolean flag;    Test2(boolean flag )    {        this.flag = flag;    }    @Override    public void run()    {        // TODO Auto-generated method stub        if (flag)        {            synchronized (MyLock.locka)            {                System.out.println(Thread.currentThread().getName()+ if locka);                synchronized (MyLock.lockb)                {                    System.out.println(Thread.currentThread().getName()+ if lockb);                }            }        }        else        {            synchronized (MyLock.lockb)            {                System.out.println(Thread.currentThread().getName()+ else lockb);                synchronized (MyLock.locka)                {                    System.out.println(Thread.currentThread().getName()+ else locka);                }            }        }    }}class MyLock{    static Object locka = new Object();    static Object lockb = new Object();}public class DieLockTest{    public static void main(String[] args)    {        Thread t1 = new Thread(new Test2(true));        Thread t2 = new Thread(new Test2(false));        t1.start();        t2.start();    }}


 

 

The program that verifies that the lock of the synchronous function is this is also deadlocked:


 

Class Test1 implements Runnable {private static int num = 500; Object obj = new Object (); boolean flag = true; public void run () {if (flag) {while (true) {synchronized (obj) // The lock is obj {show (); // The lock is this }}} else {while (true) {show ();}}} public synchronized void show () // The lock is this {synchronized (obj) // The lock is obj {if (num> = 0) {try {Thread. sleep (20);} catch (Exception e) {// TODO: handle exception System. out. println (e. toString ();} System. out. println (Thread. currentThread (). getName () + >>-- code -- + num --) ;}}} public class DieLock {public static void main (String [] args) {Test1 t = new Test1 (); Thread a = new Thread (t); Thread B = new Thread (t);. start (); try {Thread. sleep (20);} catch (Exception e) {// TODO: handle exception} t. flag = false; B. start ();}}


 

 


 

 

Related Article

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.