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,是这种情况的一个模型:

 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();
}
}

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

【翻译十二】java-并发之活性的更多相关文章

  1. 【翻译二十二】java-并发之集合与原子变量

    Concurrent Collections The java.util.concurrent package includes a number of additions to the Java C ...

  2. Java学习笔记二十二:Java的方法重写

    Java的方法重写 一:什么是方法的重写: 如果子类对继承父类的方法不满意,是可以重写父类继承的方法的,当调用方法时会优先调用子类的方法. 语法规则 返回值类型.方法名.参数类型及个数都要与父类继承的 ...

  3. 菜鸡的Java笔记 第二十二 - java 对象多态性

    本次只是围绕着多态性的概念来进行讲解,但是所讲解的代码与实际的开发几乎没有关系,而且多态一定是在继承性的基础上才可以操作的,        而本次将使用类继承的关系来描述多态的性质,实际的开发中不会出 ...

  4. 【翻译十八】java-并发之锁对象

    Lock Objects Synchronized code relies on a simple kind of reentrant lock. This kind of lock is easy ...

  5. 【翻译十四】java-并发之保护块儿

    Guarded Blocks Threads often have to coordinate their actions. The most common coordination idiom is ...

  6. Java进阶(四十二)Java中多线程使用匿名内部类的方式进行创建3种方式

    Java中多线程使用匿名内部类的方式进行创建3种方式 package cn.edu.ujn.demo; // 匿名内部类的格式: public class ThreadDemo { public st ...

  7. Java 读书笔记 (十二) Java Character 类

    在实际开发过程中, 我们经常会遇到需要使用对象,而不是内置数据类型的情况. 为了解决这个问题, Java语言为内置数据类型char提供了包装类Character类. 可以使用Character的构造方 ...

  8. java基础(十二 )-----Java泛型详解

    本文对java的泛型的概念和使用做了详尽的介绍. 概述 泛型在java中有很重要的地位,在面向对象编程及各种设计模式中有非常广泛的应用. 什么是泛型?为什么要使用泛型? 泛型,即“参数化类型”.一提到 ...

  9. Java学习笔记十二:Java中方法的重载

    Java中方法的重载 什么是方法的重载呢? 如果同一个类中包含了两个或两个以上方法名相同.方法参数的个数.顺序或类型不同的方法,则称为方法的重载,也可称该方法被重载了.如下所示 4 个方法名称都为 s ...

随机推荐

  1. C++中各种容器特点总结

    1.vector 内部数据结构:数组,可随机访问元素,在末尾增加或删除元素与元素数目无关,在其 他部分增加或删除元素随着元素数目呈线性变化. 2.deque 数组,按页/块来分配存储,每页/块包含固定 ...

  2. ubuntu 16.04 apt-get error: in the drive /media/cdrom and press

    If you have an internet connection, you can safely comment out the line starting with deb cdrom: ... ...

  3. hadoop初识

    搞什么东西之前,第一步是要知道What(是什么),然后是Why(为什么),最后才是How(怎么做).但很多开发的朋友在做了多年项目以后,都习惯是先How,然后What,最后才是Why,这样只会让自己变 ...

  4. struts2类型转换器、 类型转换错误 以及INPUT view

    1.1.1    Struts2中的类型转换器 Struts2内置了常见数据类型多种转换器 boolean 和 Boolean char和 Character int 和 Integer long 和 ...

  5. 【转】WebMagic-总体流程源码分析

    转自:http://m.blog.csdn.net/article/details?id=51943601 写在前面 前一段时间开发[知了]用到了很多技术(可以看我前面的博文http://blog.c ...

  6. jquery checkbox 复选框多次点击判断选中状态,以及全选/取消的代码示例

    2015年12月21日 10:52:51 星期一 目标, 点击当前的checbox, 判断点击后当前checkbox是否是选中状态. html: <input type="checkb ...

  7. Linux释放内存空间

    Linux服务器运行一段时间后,由于其内存管理机制,会将暂时不用的内存转为buff/cache,这样在程序使用到这一部分数据时,能够很快的取出,从而提高系统的运行效率,所以这也正是linux内存管理中 ...

  8. [ 转] [Android]多式样ProgressBar

    多式样ProgressBar 普通圆形ProgressBar 该类型进度条也就是一个表示运转的过程,例如发送短信,连接网络等等,表示一个过程正在执行中. 一般只要在XML布局中定义就可以了. < ...

  9. 3.kvm的基本管理

    1.查看KVM虚拟机配置文件 #KVM虚拟机默认配置文件位置 [root@kvm qemu]# pwd /etc/libvirt/qemu [root@kvm qemu]# ll total 12 # ...

  10. code vs1706 求合数和(数论 素数的判定)

    1706 求合数和  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 白银 Silver 题解  查看运行结果     题目描述 Description 用户输入一个数,然后输出 ...