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!");
    }
    }
    }

    }

  • 相关阅读:
    985的方格难题
    POJ 3264 区间最大最小值Sparse_Table算法
    oracle中to_date详细用法示例(oracle日期格式转换)
    PLSQL基础知识-图片
    oracle-查询-时间条件查询
    oracle基础函数--decode
    PLSQL基础学习-文字
    python3 MD5
    CentOS7关闭防火墙方法
    CentOS 7下源码安装MySQL 5.6
  • 原文地址:https://www.cnblogs.com/leigepython/p/11855600.html
Copyright © 2011-2022 走看看