关闭

读写死锁

238人阅读 评论(0) 收藏 举报

public class DeadlockRisk {
    private static class Resource {
        public int value;
    }

    private Resource resourceA = new Resource();
    private Resource resourceB = new Resource();

    public int read() {
        synchronized (resourceA) {
            synchronized (resourceB) {
                return resourceB.value + resourceA.value;
            }
        }
    }

    public void write(int a, int b) {
        synchronized (resourceB) {
            synchronized (resourceA) {
                resourceA.value = a;
                resourceB.value = b;
            }
        }
    }
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:86600次
    • 积分:1892
    • 等级:
    • 排名:千里之外
    • 原创:114篇
    • 转载:0篇
    • 译文:0篇
    • 评论:9条
    文章分类
    最新评论