The Dining Philosophers Problem
$B?);v$r$9$kE/3X

Revised: May/3rd/2002

$B$3$3$G$O;q8;6&M-LdBj$H$7$FM-L>$J!V?);v$r$9$kE/3XR2p$7$^$9!#(B

$BK\9F$G$O%F%-%9%H%Y!<%9$N%"%W%j%1!<%7%g%s$H$7$F:n@.$7$^$9$,!"(B Sun Microsystems $B$N(B Tutorial $B$G$O(B GUI $B%"%W%l%C%H$H$7$F%5%s%W%k$,8x3+$5$l$F$$$^$9!#%3!<%I$rFI$`NO$N$"$kJ}$O;2>H$9$k$HNI$$$G$7$g$&!#(B

$B%7%J%j%*(B

$B#5?M$NE/3Xl9g$O!"$=$N$^$^%U%)!<%/$,6u$/$N$rBT$C$F52$($F$7$^$&!#(B

$BA40w$NE/3Xe$K6u$$$?%U%)!<%/$,$J$/$J$C$F$7$^$&$N$G!"%U%)!<%/$,6u$/$N$rBT$C$?$^$^$@$l$b?);v$,$G$-$:$K52$(B3$1$F$7$^$&!#(B

$B%/%i%935MW(B

$BF14|;XDj$5$l$?%3!<%I$r;}$D8^$D$N6&M-%*%V%8%'%/%H$H!"$=$l$rMxMQ$9$k8^$D$N%9%l%C%I$NLdBj$H$7$F9M$($^$9!#(B

$B6&M-%*%V%8%'%/%H(B: Fork

int id
$B$I$NE/3X
boolean eating
$BE/3X
void pick(int i)
$BF14|;XDj$7$F#2$D0J>e$N%9%l%C%I$K%"%/%;%9$5$l$J$$$h$&$K$7$F$*$/!#%a%=%C%I0z?t$O%U%)!<%/$reating $B$,(B true $B$N4V$OB>$NE/3Xwait()$B!#(Bfalse $B$J$i$P(B true $B$r%;%C%H!#(B
void down()
$BF14|;XDj$7$?%3!<%I$G!"(B eating $B$K(B false $B$r%;%C%H!#(B

$B%9%l%C%I(B: Phylosopher

int id
$BE/3X
int eatTime
$B?);v$KHq$d$9;~4V!#(B
int thinkTime
$B;W:w$KHq$d$9;~4V!#(B
int left
$B:8id $B$rBeF~!#(B
int right
$B1&id - 1 $B$rBeF~!#C"$7!"(Bid $B$,(B 0 $B$NE/3X4$B!#(B
Fork[] forks
$B6&M-%*%V%8%'%/%H!#MWAG?t$O(B 5$B!#(B
void setProperties
$B0J>e$NJQ?t$KCM$r%;%C%H!#(B
void feelHungry()
Fork $B7?%*%V%8%'%/%H$G(B id $B$,(B left, right $B$N$b$N$r8F$V!#N>J}$N=hM}$,=*N;$7$?$i%a%C%;!<%8$r=PNO$7$F(B eatTime $B$@$1(B sleep() $B$7$?$"$H:81&$N%U%)!<%/$r(B down()$B!#(B
void think()
$B%a%C%;!<%8$r=PNO$7$F(B thinkTime $B$@$1(B sleep()$B!#(B
void run()
feelHungry() $B$H(B think() $B$r7+$jJV$78F$V!#(B

$B%5%s%W%k(B

// $B6&M-%*%V%8%'%/%H(B
class Fork {
	// $B$I$NE/3Xpick(int i) {
		while (eating == true) {
		// $BNY$NE/3Xwait();
			} catch (InterruptedException e) {
				System.out.println(e);
			}
		}
		// $Bdown() {
		// $B?);v$,:Q$s$@$i(B false $B$r%;%C%H(B
		eating = false;
		// $BBT5!%W!<%k$N%9%l%C%I$r%m%C%/C5:w>uBV$K(B
		notifyAll();
	}
}
class Philosopher implements Runnable {
	int id;       	// $BE/3XfeelHungry() {
		// $B:8forks[left].pick(id);
		
		// $B$3$3$NBT5!;~4V$,D9$$$H%G%C%I%m%C%/H/@8(B
		try {
			Thread.sleep(500);
		} catch (InterruptedException e) {
			System.out.println(e);
		}
		
		// $B1&forks[right].pick(id);
		
		System.out.println(id + " is eating.");
		try {
			// $B?);vCf(B
			Thread.sleep(eatTime);
		} catch (InterruptedException e) {
			System.out.println(e);
		}
		// $B?);v=*N;(B
		// $B:8forks[left].down();
		// $B1&forks[right].down();
	}
	
	// $B;W:w(B
	public void think() {
		try {
			// $B;W:wCf(B
			Thread.sleep(thinkTime);
		} catch (InterruptedException e) {
			System.out.println(e);
		}
	}
	
	// $B%9%l%C%I$N(B run() $B%a%=%C%I(B
	public void run() {
		while (true) {
			// $B;W:wCf(B
			System.out.println(id + " is thinking.");
			think();
			// $B6uJ"$r46$8$k(B
			System.out.println(id + " feels hungry.");
			feelHungry();
		}
	}
}

$B$3$l$i$N%/%i%9$rMxMQ$9$k%3%s%H%m!<%k%/%i%9$O

class Dining {
	public static void main(String[] args) {
		// $B6&M-%*%V%8%'%/%H(B
		Fork[] forks = new Fork[5];
		// $BE/3X2000,   1000,     forks);
		phils[1].setProperties(1900,   1100,     forks);
		phils[2].setProperties(1800,   1200,     forks);
		phils[3].setProperties(1700,   1300,     forks);
		phils[4].setProperties(1600,   1400,     forks);
		
		// $B%9%l%C%I(B
		Thread[] thres = new Thread[5];
		for (int i=0; i<5; i++) {
			// $BE/3Xy(B
			thres[i] = new Thread(phils[i]);
		}
		
		// $B%9%l%C%I$N3+;O(B
		for (int i=0; i<5; i++) {
			thres[i].start();
		}
	}
}

$BA4$F$NE/3Xe$NNc$N>l9g$O!";W:w$+$i3P$a$?E/3X

Philosopher $B%/%i%9$N(B feelHunger() $B%a%=%C%I$G!":8sleep() $B$G(B 500 $B$K;XDj$7$F$$$k!#$3$NCM$r>.$5$/$9$l$P%G%C%I%m%C%/$O5/$3$i$J$$!#E/3X

Deadlock $B$,H/@8$7$?

C:\java\thread>javac Dining.java
C:\java\thread>java Dining
0 is thinking.
1 is thinking.
2 is thinking.
3 is thinking.
4 is thinking.
0 feels hungry.
1 feels hungry.
2 feels hungry.
3 feels hungry.
4 feels hungry.
0 is starving.
1 is starving.
2 is starving.
3 is starving.
4 is starving.

$B6/@)=*N;$O(B [Ctrl + C] $B$G$9!#(B



Copyright © 2002 SUGAI, Manabu. All Rights Reserved.
SEO [PR] 爆速!無料ブログ 無料ホームページ開設 無料ライブ放送