【翻译十二】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, , models this possibility: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();
}
}
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-并发之活性的更多相关文章
- 【翻译二十二】java-并发之集合与原子变量
Concurrent Collections The java.util.concurrent package includes a number of additions to the Java C ...
- Java学习笔记二十二:Java的方法重写
Java的方法重写 一:什么是方法的重写: 如果子类对继承父类的方法不满意,是可以重写父类继承的方法的,当调用方法时会优先调用子类的方法. 语法规则 返回值类型.方法名.参数类型及个数都要与父类继承的 ...
- 菜鸡的Java笔记 第二十二 - java 对象多态性
本次只是围绕着多态性的概念来进行讲解,但是所讲解的代码与实际的开发几乎没有关系,而且多态一定是在继承性的基础上才可以操作的, 而本次将使用类继承的关系来描述多态的性质,实际的开发中不会出 ...
- 【翻译十八】java-并发之锁对象
Lock Objects Synchronized code relies on a simple kind of reentrant lock. This kind of lock is easy ...
- 【翻译十四】java-并发之保护块儿
Guarded Blocks Threads often have to coordinate their actions. The most common coordination idiom is ...
- Java进阶(四十二)Java中多线程使用匿名内部类的方式进行创建3种方式
Java中多线程使用匿名内部类的方式进行创建3种方式 package cn.edu.ujn.demo; // 匿名内部类的格式: public class ThreadDemo { public st ...
- Java 读书笔记 (十二) Java Character 类
在实际开发过程中, 我们经常会遇到需要使用对象,而不是内置数据类型的情况. 为了解决这个问题, Java语言为内置数据类型char提供了包装类Character类. 可以使用Character的构造方 ...
- java基础(十二 )-----Java泛型详解
本文对java的泛型的概念和使用做了详尽的介绍. 概述 泛型在java中有很重要的地位,在面向对象编程及各种设计模式中有非常广泛的应用. 什么是泛型?为什么要使用泛型? 泛型,即“参数化类型”.一提到 ...
- Java学习笔记十二:Java中方法的重载
Java中方法的重载 什么是方法的重载呢? 如果同一个类中包含了两个或两个以上方法名相同.方法参数的个数.顺序或类型不同的方法,则称为方法的重载,也可称该方法被重载了.如下所示 4 个方法名称都为 s ...
随机推荐
- TP中的四种url访问方式
什么是PATHINFO:就是http://localhost/index.php/Home/Index/index/a/1/b/2?c=3中的红色部分, 注意c=3并不是pathinfo的一部分,它是 ...
- ubuntu配置apache的虚拟主机
ubuntu中apache的配置文件分散在几个文件中,/etc/apache2/apache2.conf将它们组织起来.这样设计有很多好处,这里就不在赘述了.进入正题: 1)配置文件在/etc/apa ...
- BurpSuite使用设置
一.设置字体 二.设置字符集 三.设置浏览器代理 四.BurpSuite访问步骤 五.在Target中可以查看截获的数据包
- MyEclipse 优化
1.取消自动validation 有一堆,什么xml.jsp.jsf.js等等, 我们没有必要全部都去自动校验一下,只是需要的时候才会手工校验一下! 取消方法: windows-->perfer ...
- Objective C 快速入门学习三
1.数据类型 和C语言基本一样. 有一个特别数据类型id,可以储存任何类型的对象,它是实现多态和动态绑定的基础. Objective-C 2.程序结构 Objective-C和C的程序结构一模一样,具 ...
- java servlet的工作原理
servlet本质上就是java类嘛.不过是有特殊规范的java类而已.下面就说一说为什么servlet要有特殊规范. 首先,考虑一下什么地方用servlet,WEB应用,而且是需要servlet容器 ...
- C++ 代码头注释模板
/********************************************************************************* *Copyright(C),You ...
- Wince下sqlce数据库开发(二)
上次写到使用数据绑定的方法测试本地sqlce数据库,这次使用访问SQL Server的方法访问sqlce,你会发现他们是如此的相似... 参考资料:http://www.cnblogs.com/rai ...
- centos6.5 iptables结合ipset批量屏蔽ip
安装ipset yum install ipset #创建ip地址集合 ipset create bansms hash:net 查找访问了“getVerificationCode”并且次数大于10次 ...
- shell 脚本编程概述
环境变量 ? 退出状态码 (成功) (未知错误) (误用 shell 命令) (命令不可执行) (没找到命令) (无效退出状态) +x( linux 信号 X 的严重错误) ( ctrl c 终止程序 ...