• 【翻译十二】java-并发之活性


    A concurrent application's ability to execute in a timely manner is known as its liveness. This section describes the most common kind of liveness problem, deadlock, and goes on to briefly describe two other liveness problems, starvation and livelock.

    译文:

      一个并发应用程序在时间上的执行的能力被称为活动性。这一节主要描述了活动性的问题,死锁,并简要的介绍两个其他的活动性问题,饥饿和活锁。

    Deadlock

    Deadlock describes a situation where two or more threads are blocked forever, waiting for each other. Here's an example.

    Alphonse and Gaston are friends, and great believers in courtesy. A strict rule of courtesy is that when you bow to a friend, you must remain bowed until your friend has a chance to return the bow. Unfortunately, this rule does not account for the possibility that two friends might bow to each other at the same time. This example application, Deadlock, models this possibility:

    public class Deadlock {
        static class Friend {
            private final String name;
            public Friend(String name) {
                this.name = name;
            }
            public String getName() {
                return this.name;
            }
            public synchronized void bow(Friend bower) {
                System.out.format("%s: %s"
                    + "  has bowed to me!%n", 
                    this.name, bower.getName());
                bower.bowBack(this);
            }
            public synchronized void bowBack(Friend bower) {
                System.out.format("%s: %s"
                    + " has bowed back to me!%n",
                    this.name, bower.getName());
            }
        }
    
        public static void main(String[] args) {
            final Friend alphonse =
                new Friend("Alphonse");
            final Friend gaston =
                new Friend("Gaston");
            new Thread(new Runnable() {
                public void run() { alphonse.bow(gaston); }
            }).start();
            new Thread(new Runnable() {
                public void run() { gaston.bow(alphonse); }
            }).start();
        }
    }
    

    When Deadlock runs, it's extremely likely that both threads will block when they attempt to invoke bowBack. Neither block will ever end, because each thread is waiting for the other to exit bow.

    译文:

    死锁

    死锁描述了一种两个或者两个以上的线程互相阻塞的一种现象,互相等待,下面是一个例子。

    Alphonse 和Gaston 是朋友,并且都是伟大的礼貌信徒。礼貌有一个严格的规定,当你向你的朋友鞠躬的时候,你必须保持鞠躬,知道你朋友有机会能够给你回敬一个鞠躬。不幸的是,这种规则没有考虑到两个人同时鞠躬的情况。下面这个实例,DeadLock,是这种情况的一个模型:

     1 public class Deadlock {
     2     static class Friend {
     3         private final String name;
     4         public Friend(String name) {
     5             this.name = name;
     6         }
     7         public String getName() {
     8             return this.name;
     9         }
    10         public synchronized void bow(Friend bower) {
    11             System.out.format("%s: %s"
    12                 + "  has bowed to me!%n", 
    13                 this.name, bower.getName());
    14             bower.bowBack(this);
    15         }
    16         public synchronized void bowBack(Friend bower) {
    17             System.out.format("%s: %s"
    18                 + " has bowed back to me!%n",
    19                 this.name, bower.getName());
    20         }
    21     }
    22 
    23     public static void main(String[] args) {
    24         final Friend alphonse =
    25             new Friend("Alphonse");
    26         final Friend gaston =
    27             new Friend("Gaston");
    28         new Thread(new Runnable() {
    29             public void run() { alphonse.bow(gaston); }
    30         }).start();
    31         new Thread(new Runnable() {
    32             public void run() { gaston.bow(alphonse); }
    33         }).start();
    34     }
    35 }

    当DeadLock运行时,它极有可能使两个线程在调用bowBack()的时候阻塞。阻塞永远不会消失,因为它们都在等待对方退出Blow()方法。

    简历: 三年国内著名软件公司Java信息系统开发经验。熟悉税务相关业务知识。一年以上互联网公司工作经验。 目标职位: Java研发工程师、大数据、推荐算法等。 目标城市: 北京,杭州,沈阳。 联系方式: 邮箱:hecuking@126.com
  • 相关阅读:
    linux安装kafka教程
    linux 系统java相关部署
    redies学习总结
    Sentinel自定义异常降级-新旧版本差异
    Android Bitmap压缩详解
    Head First之策略模式
    go测试
    go获取命令行参数
    JVM-垃圾收集算法基础
    Java代理模式
  • 原文地址:https://www.cnblogs.com/accipiter/p/3231399.html
Copyright © 2020-2023  润新知