文档介绍:哲学家问题的解决
复现
筷子
publicclassChopsticks{
哲学家
publicclassPhilosopher{
publicPhilosopher(Stringname,Chopsticksleft,Chop哲学家问题的解决
复现
筷子
publicclassChopsticks{
哲学家
publicclassPhilosopher{
publicPhilosopher(Stringname,Chopsticksleft,Chopsticksright){=name;
=left;=right;:…左手筷子privateChopsticksleft;右手筷子privateChopsticksright;
publicvoideating(){
synchronized(left){
try{
Threadsleep(1000);
}catch(InterruptedExceptione){
();
synchronized(right){
try{
Threadsleep(1000);
}catch(InterruptedExceptione){
();
(+"iseating...");
主函数
publicclassMain{
publicstaticvoidmain(String[]args){Chopstickscl=newChopsticks();Chopsticksc2=newChopsticks();Chopsticksc3=newChopsticks();Chopsticksc4=newChopsticks();Chopsticksc5=newChopsticks();
分配筷子
Philosopherp1=newPhilosopher("p1",c1,c2);Philosopherp2=newPhilosopher("p2",c2,c3);Philosopherp3=newPhilosopher("p3",c3,c4);Philosopherp4=newPhilosopher("p4",c4,c5);Philosopherp5=newPhilosopher("p5",c5,c1);
newThread(()->{
();
}).start();
newThread(()->{();
}).start();
newThread(()->{
();
}).start();
newThread(()->{
();
}).start();
newThread(()->{();
}).start();
}
使用时先分配左手,再分配右手,产生了死锁问题。使用DD查看
先获取进程号
根据进程号查看死锁装态
死锁的详细信息
FcuiidoneJava-leveldeadlocL:
“TlweBidT":
waitingtoLochimnitorBx^BBO