Starvation in java

本文通过一个具体的Java示例展示了并发编程中的饥饿问题。在该示例中,某些线程由于资源被其他线程长期占用而无法获取到必要的资源,从而导致它们无法继续执行。

Continued the article of Livelock in java, now I will demonstrate a example of starvation in java.

Starvation and livelock are much less common a problem than deadlock, but are still problems that every designer of concurrent software is likely to encounter.

Starvation

Starvation describes a situation where a thread is unable to gain regular access to shared resources and is unable to make progress. This happens when shared resources are made unavailable for long periods by "greedy" threads. For example, suppose an object provides a synchronized method that often takes a long time to return. If one thread invokes this method frequently, other threads that also need frequent synchronized access to the same object will often be blocked.

[@more@]

package com.herosoft.Concurrency;

import java.util.HashMap;
import java.util.Map;

public class Starvation {


static class Order {
private static int i = 0;
private int count = i++;
boolean eating;
public Order() {
if(count == 10) {
System.out.println("Out of food, closing");
System.exit(0);
}
}
public void consume() {
i--;
}
public String toString() { return "Order " + count; }
}

static class WaitPerson extends Thread {
private Restaurant restaurant;

public WaitPerson(Restaurant restaurant) {
this.restaurant=restaurant;
//start();
}
public void run() {
int i = ((Integer)restaurant.m.get(this)).intValue()+1;
System.out.println("the " + i + " rd waitperson goes into this restaurant");
while(true) {
while(restaurant.order==null || restaurant.order.count != ((Integer)restaurant.m.get(this)).intValue())
synchronized(restaurant.chef) {
try {
restaurant.chef.wait();
} catch(InterruptedException e) {
throw new RuntimeException(e);
}

}
restaurant.order.eating = true;
System.out.println("the " + i +
" rd waitperson got " + restaurant.order);
restaurant.order.consume();
restaurant.order.eating =false;
restaurant.order = null;


}
}
}

static class Chef extends Thread {
public Restaurant restaurant;
public Chef(Restaurant r) {
restaurant = r;
start();
}
public void run() {
while(true) {
if(restaurant.order == null) {
restaurant.order = new Order();
System.out.print("Order up! ");
synchronized(this) {
this.notify();
}
}else {
if(!restaurant.order.eating ) {
synchronized(this) {
this.notify();
}
}
}
try {
sleep(100);
} catch(InterruptedException e) {
throw new RuntimeException(e);
}
}
}
}

static class Restaurant {
Order order; // Package access
Chef chef;
WaitPerson[] persons;

Map m = new HashMap();


@SuppressWarnings("unchecked")
public void set(Chef chef, WaitPerson...persons) {
this.chef=chef;
this.persons=persons;
int i=0;
for(WaitPerson w: persons)
m.put(w, i++);
}
} ///:~



/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Restaurant restaurant = new Restaurant();
Chef chef = new Chef(restaurant);
WaitPerson waitPerson = new WaitPerson(restaurant);
WaitPerson waitPerson2 = new WaitPerson(restaurant);//this two waitperson will never get the order that is supposed to offer to them
WaitPerson waitPerson3 = new WaitPerson(restaurant);

restaurant.set(chef,
new WaitPerson[]{waitPerson, waitPerson2, waitPerson3});

waitPerson.start();
waitPerson2.start();
waitPerson3.start();

}

}

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/220284/viewspace-1031608/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/220284/viewspace-1031608/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值