zoukankan      html  css  js  c++  java
  • java(jdk8)死锁案例

    package testSynchorized;

    public class SynchronizedDeadLockTest {
    static Object src1 = new Object();
    static Object src2 = new Object();

    public static void main(String[] args) {
    Thread t1 = new Thread(new DeadARunnable(), "t1");
    Thread t2 = new Thread(new DeadBRunnable(), "t2");
    t1.start();
    t2.start();
    }
    }

    class DeadARunnable implements Runnable {
    @Override
    public void run() {
    synchronized (SynchronizedDeadLockTest.src1) {
    try {
    System.out.println(Thread.currentThread().getName() + " get src1 ing!");
    Thread.sleep(500);
    System.out.println(Thread.currentThread().getName() + " after sleep 500ms!");
    } catch (Exception e) {
    e.printStackTrace();
    }
    System.out.println(Thread.currentThread().getName() + " need src2 and waiting!");
    /**
    * Thread t2 try get source src2 when locked source src1
    */
    synchronized (SynchronizedDeadLockTest.src2) {
    System.out.println(Thread.currentThread().getName() + " get src2 ing!");
    }
    }
    }
    }

    class DeadBRunnable implements Runnable {
    @Override
    public void run() {
    synchronized (SynchronizedDeadLockTest.src2) {
    try {
    System.out.println(Thread.currentThread().getName() + " get src2 ing!");
    Thread.sleep(500);
    System.out.println(Thread.currentThread().getName() + " after sleep 500ms!");
    } catch (Exception e) {
    e.printStackTrace();
    }
    System.out.println(Thread.currentThread().getName() + " need src1 and waiting!");
    /**
    * Thread t1 try get source src1 when locked source src2
    */
    synchronized (SynchronizedDeadLockTest.src1) {
    System.out.println(Thread.currentThread().getName() + " get src1 ing!");
    }
    }
    }

    }

  • 相关阅读:
    ios中的XMPP简介
    iOS项目开发中的目录结构
    ios中怎么样点击背景退出键盘
    ios中怎么处理键盘挡住输入框
    ios中怎么样调节占位文字与字体大小在同一高度
    ios中怎么样设置drawRect方法中绘图的位置
    ios中用drawRect方法绘图的时候设置颜色
    字符串常见操作
    字典、列表、元组
    字符串查看及应用
  • 原文地址:https://www.cnblogs.com/leigepython/p/11855600.html
Copyright © 2011-2022 走看看