【翻译十二】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 ...
随机推荐
- BZOJ 2177: 曼哈顿最小生成树
Sol 考了好几次曼哈顿最小生成树,然而一直不会打...这次终于打出来了...神tm调试了2h...好蛋疼... 首先曼哈顿最小生成树有个结论就是讲它每45度分出一个象限,对于每个点,只与每个象限中离 ...
- 【云计算】docker三剑客如何支持分布式部署?
This blog will explain how to create multi-container application deployed on multiple hosts using Do ...
- 多线程同步_Monitor
多线程一直在学习和理解中...... Monitor类是多线程中用以实现同步的一种技术,主要是同一进程内多线程间的同步技术. Monitor类中有以下几个方法需要注意: Monitor.Enter(o ...
- FFmpeg frei0r water 滤镜
FFmpeg frei0r water 滤镜, 在 linux 环境中很流畅,但在 XP 环境中抛出异常 研究一段时间修改了代码,能在 XP 里跑得动. sample.water.avi water. ...
- Java for LeetCode 210 Course Schedule II
There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...
- ABAP 行列稳定刷新语句
DATA stbl TYPE lvc_s_stbl. stbl-row = 'X'." 基于行的稳定刷新 stbl-col = 'X'." 基于列稳定刷新 ...
- NSBundle的pathForResource:ofType: 返回值为nil问题
一.问题描述 在处理XML的过程中,在项目中手动创建名为“data.xml”的文件,加入网上找的内容 <?xml version="1.0" encoding="u ...
- jQueryUI Datepicker的使用
jQueryUI Datepicker是一个高度可定制的插件,可以很方便的为你的页面添加日期选择功能,你可以自定义日期的显示格式 以及要使用的语言. 你可以使用键盘的快捷键来驱动datepicker插 ...
- Tomcat中文乱码问题的原理和解决方法
1) 更改 D:\Tomcat\conf\server.xml,指定浏览器的编码格式为“简体中文”: 方法是找到 server.xml 中的 <Connector port="8080 ...
- 关于java中Double类型的运算精度问题
标题 在Java中实现浮点数的精确计算 AYellow(原作) 修改 关键字 Java 浮点数 精确计算 问题的提出:如果我们编译运行下面这个程序会看到什么?publi ...