zoukankan      html  css  js  c++  java
  • 使用线程模拟死锁情形

    (转自南瓜皮的网屋)

    死锁问题是并发处理的一种典型错误情况,下面的代码演示了这个情形:

    public class TestDeadLock implements Runnable {

    public int flag = 1;

    static Object o1 = new Object(), o2 = new Object();

    public void run() {

    System.out.println("flag=" + flag);

    if(flag == 1) {

    synchronized(o1) {

    try {

    Thread.sleep(500);

    } catch (Exception e) {

    e.printStackTrace();

    }

    synchronized(o2) {

    System.out.println("1");

    }

    }

    }

    if(flag == 0) {

    synchronized(o2) {

    try {

    Thread.sleep(500);

    } catch (Exception e) {

    e.printStackTrace();

    }

    synchronized(o1) {

    System.out.println("0");

    }

    }

    }

    }

    public static void main(String[] args) {

    TestDeadLock td1 = new TestDeadLock();

    TestDeadLock td2 = new TestDeadLock();

    td1.flag = 1;

    td2.flag = 0;

    Thread t1 = new Thread(td1);

    Thread t2 = new Thread(td2);

    t1.start();

    t2.start();

    }

    }

  • 相关阅读:
    Docker
    CTF各种资源:题目、工具、资料
    Android工具集合
    Android相关资源
    命令注入新玩法:巧借环境攻击目标
    分库分表
    数据库读写分离
    Insomni'hack teaser 2019
    Insomni'hack teaser 2019
    35C3 CTF
  • 原文地址:https://www.cnblogs.com/pxsbest/p/2631698.html
Copyright © 2011-2022 走看看