java简单实现死锁

 

 

package com.cdzyjsxy.javase.deadlock;

/**
 * 死锁
 *
 * @author FL
 * @date 2020/7/8
 **/
public class DeadLock {
    public static void main(String[] args) {
        Object o1 = new Object();
        Object o2 = new Object();
        Thread t1 = new MyThread1(o1, o2);
        Thread t2 = new MyThread2(o1, o2);
        t1.start();
        t2.start();

    }
}

class MyThread1 extends Thread {
    Object o1;
    Object o2;

    public MyThread1(Object o1, Object o2) {
        this.o1 = o1;
        this.o2 = o2;
    }

    @Override
    public void run() {
        synchronized (o1) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            synchronized (o2) {

            }
        }

    }
}

class MyThread2 extends Thread {
    Object o1;
    Object o2;

    public MyThread2(Object o1, Object o2) {
        this.o1 = o1;
        this.o2 = o2;
    }

    @Override
    public void run() {
        synchronized (o2) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            synchronized (o1) {

            }
        }
    }
}
死锁避免是一种预防死锁的策略,在资源申请时通过安全性检查来避免系统进入死锁状态。下面是一个简单Java实现: ```java public class DeadlockAvoidanceSystem { private int[][] allocation; private int[][] max; private int[] available; private int[] work; private boolean[] finish; public DeadlockAvoidanceSystem(int[][] allocation, int[][] max, int[] available) { this.allocation = allocation; this.max = max; this.available = available; this.work = available.clone(); this.finish = new boolean[allocation.length]; } public boolean requestResources(int processId, int[] request) { if (lessEqual(request, need(processId)) && lessEqual(request, available)) { // 试探性地分配资源 for (int i = 0; i < request.length; i++) { allocation[processId][i] += request[i]; available[i] -= request[i]; need(processId)[i] -= request[i]; } // 检查是否安全 if (isSafe()) { return true; } else { // 回滚分配 for (int i = 0; i < request.length; i++) { allocation[processId][i] -= request[i]; available[i] += request[i]; need(processId)[i] += request[i]; } } } return false; } private boolean isSafe() { for (int i = 0; i < allocation.length; i++) { finish[i] = false; } int count = 0; while (count < allocation.length) { boolean find = false; for (int i = 0; i < allocation.length; i++) { if (!finish[i] && lessEqual(need(i), work)) { find = true; for (int j = 0; j < work.length; j++) { work[j] += allocation[i][j]; } finish[i] = true; count++; } } if (!find) { return false; // 找不到满足条件的进程,说明不安全 } } return true; } private int[] need(int processId) { int[] need = new int[max[processId].length]; for (int i = 0; i < max[processId].length; i++) { need[i] = max[processId][i] - allocation[processId][i]; } return need; } private boolean lessEqual(int[] a, int[] b) { for (int i = 0; i < a.length; i++) { if (a[i] > b[i]) { return false; } } return true; } } ``` 使用时,可以按照以下步骤: 1. 定义进程数、资源数、分配矩阵、最大需求矩阵和可用资源向量。 2. 创建`DeadlockAvoidanceSystem`对象,传入上述参数。 3. 调用`requestResources()`方法来申请资源,如果返回true,则表示申请成功,否则失败。 示例代码: ```java public static void main(String[] args) { int[][] allocation = {{0, 1, 0}, {2, 0, 0}, {3, 0, 2}, {2, 1, 1}, {0, 0, 2}}; int[][] max = {{7, 5, 3}, {3, 2, 2}, {9, 0, 2}, {2, 2, 2}, {4, 3, 3}}; int[] available = {3, 3, 2}; DeadlockAvoidanceSystem system = new DeadlockAvoidanceSystem(allocation, max, available); int[] request = {0, 0, 2}; if (system.requestResources(2, request)) { System.out.println("Request success!"); } else { System.out.println("Request failed!"); } } ``` 注意:这只是一个简单死锁避免系统的实现,实际上死锁避免是一个很复杂的问题,在实际应用中还需要考虑许多因素,如系统资源的动态分配、进程优先级等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值