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

被折叠的 条评论
为什么被折叠?



