`
623deyingxiong
  • 浏览: 190150 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

活性(livness)(Concurrency Tutorial 4)

阅读更多
活性(Liveness)

并发程序能及时(in a timely manner)执行的能力叫作它的活性。本节描述了最常见的一种活性问题:死锁(deadlock)。然后简单描述了另外两个活性问题, 饥饿(starvation)和活锁(livelock)。

死锁

死锁描述了两个或更多的线程因为要等待彼此释放锁而永远阻塞。这里有个例子:

Alphonse和Gaston是朋友,很重视礼节(great believers in courtesy)。一个很重要的约定是,当向一个朋友鞠躬时,如果朋友不向你回礼(鞠躬)你就不能直身。不幸的是,如果两个人同时向对方鞠躬就有问题了。在本例中,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();
    }
}


当死锁发生时,很可能是这样的情形:两个线程都因试图调用对方的bowBack而阻塞。两个阻塞都不会停止,因为每个线程都在等待对方停止对bow的调用。(译者注:若双方行为结束的条件是对方结束行为,那么双方的行为都将无限等待下去。)

饥饿(Starvation) and 活锁(Livelock)

Starvation 和 Livelock 与死锁相比较而言是非常少见的活性问题,但并发软件的设计者依然会遇到。

饥饿

Starvation描述了一种情形,一个线程没有机会获得共享资源也无法取得进展。如果有“贪婪”的线程长期占有共享资源,这就有可能发生。例如,假设一个对象提供了一个需要很长时间才能退出的同步方法。如果某一线程经常调用此方法,那其他调用该对象其他同步方法或以该对象作为锁提供者的同步代码,就会经常阻塞。

活锁

若有两个线程,它们的操作都需要对另一线程的操作做出回应,此时活锁会发生。就像死锁一样,活锁线程无法取得进展。然而,线程不是阻塞的——它们只是因为太忙于向彼此回复而无暇工作。这就像(This is comparable to)两个人在走廊里都试图给对方让路(pass each other):Alphonse 移到他的左边让Gaston过,而Gaston移到他的右边让Alphonse过,当看到他们仍在阻塞对方,Alphone移到他的右边,而Gaston移到他的左边。他们仍在阻塞,然后…

(以下是译者加的活锁例子)
package com.test.livelock;

public class Gentleman extends Thread {
	private String manName;
	private Gentleman anotherGentleman;

	public Gentleman(String name,Gentleman anotherGentleman){
		this.manName=name;
		this.anotherGentleman=anotherGentleman;
	}
	
	public Gentleman() {
	}
	void walkwith(Gentleman anotherGentleman){
		try {
			System.out.println(manName+" says :i pass "+anotherGentleman.getManName());
			anotherGentleman.join();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println(manName+" says : i passed");
	}
	
	private String getManName() {
		return manName;
	}
	

	public Gentleman getAnotherGentleman() {
		return anotherGentleman;
	}

	public void setAnotherGentleman(Gentleman anotherGentleman) {
		this.anotherGentleman = anotherGentleman;
	}

	public void setManName(String manName) {
		this.manName = manName;
	}

	@Override
	public void run() {
		walkwith(anotherGentleman);
	}
	
	public static void main(String[] args) {
		Gentleman jay=new Gentleman();
		Gentleman jj=new Gentleman();
		
		jay.setAnotherGentleman(jj);
		jj.setAnotherGentleman(jay);
		
		jay.setManName("Jay");
		jj.setManName("JJ");
		
		jay.start();
		jj.start();
	}
}
分享到:
评论

相关推荐

    Python Concurrency with asyncio

    Python Concurrency with asyncio

    Java 9 Concurrency Cookbook.2e

    This book covers the most important and useful mechanisms included in version 9 of the Java concurrency API, so you will be able to use them directly in your applications. The mechanisms are as ...

    Java Concurrency in Practice

    Java Concurrency in practice

    C++ Concurrency in Action 2nd.pdf

    《C++ Concurrency in Action》第二版是一本专注于C++编程语言中并发和并行编程的权威指南。本书主要侧重于C++11以及后续版本中引入的多线程功能,旨在帮助读者深入理解并有效运用C++并发特性。由于C++11对并发的...

    C++ Concurrency in Action 2nd Edition

    This book is an in-depth guide to the concurrency and multithreading facilities from the new C++ Standard, from the basic usage of std::thread, std::mutex, and std:: async, to the complexities of ...

    Effective Concurrency

    including the widely-cited essay "The Free Lunch Is Over" which coined the term "concurrency revolution" to describe the software sea change now in progress to exploit increasingly parallel hardware....

    Java-Concurrency-Essentials

    Concurrency is always a challenge for developers and writing concurrent programs can be extremely hard. There is a number of things that could potentially blow up and the complexity of systems rises ...

    Java Concurrency in Practice 无水印pdf

    Java Concurrency in Practice 英文无水印pdf pdf所有页面使用FoxitReader和PDF-XChangeViewer测试都可以打开 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者...

    Concurrency in Go: Tools and Techniques for Developers

    Concurrency in Go: Tools and Techniques for Developers by Katherine Cox-Buday English | 19 July 2017 | ISBN: 1491941197 | ASIN: B0742NH2SG | 240 Pages | AZW3 | 1.15 MB Concurrency can be notoriously...

    Java.Threads.and.the.Concurrency.Utilities.1484216997

    Chapter 4: Additional Thread Capabilities Part II: Concurrency Utilities Chapter 5: Concurrency Utilities and Executors Chapter 6: Synchronizers Chapter 7: The Locking Framework Chapter 8: Additional...

    Concurrency in Go.pdf

    4. 并发难度问题(Concurrency Is Hard)指出了在计算机科学中实现并发的困难。并发编程比顺序编程复杂得多,需要考虑数据竞态(Race Conditions)、原子性(Atomicity)和内存一致性等问题。 5. 数据竞态(Race ...

Global site tag (gtag.js) - Google Analytics