Revised: May/3rd/2002
$B$3$3$G$O;q8;6&M-LdBj$H$7$FM-L>$J!V?);v$r$9$kE/3X
$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#5?M$NE/3X
$BA40w$NE/3X
$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
Fork
int id
boolean eating
void pick(int i)
true
$B$N4V$OB>$NE/3Xfalse
$B$J$i$P(B true
$B$r%;%C%H!#(Bvoid down()
eating
$B$K(B false
$B$r%;%C%H!#(BPhylosopher
int id
int eatTime
int thinkTime
int left
int right
id
$B$,(B 0
$B$NE/3XFork[] forks
5
$B!#(Bvoid setProperties
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!#(Bvoid think()
thinkTime
$B$@$1(B sleep()
$B!#(Bvoid run()
feelHungry()
$B$H(B think()
$B$r7+$jJV$78F$V!#(B// $B6&M-%*%V%8%'%/%H(B class Fork { // $B$I$NE/3Xpick(int i) { while (eating == true) { // $BNY$NE/3X wait(); } catch (InterruptedException e) { System.out.println(e); } } // $B down() { // $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:8 forks[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:8 forks[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/3X y(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/3X Deadlock $B$,H/@8$7$? $B6/@)=*N;$O(B [Ctrl + C] $B$G$9!#(BPhilosopher
$B%/%i%9$N(B feelHunger()
$B%a%=%C%I$G!":8
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.
Copyright © 2002 SUGAI, Manabu. All Rights Reserved.
SEO
[PR] 爆速!無料ブログ 無料ホームページ開設 無料ライブ放送