实验六互斥算法实现(一)(验证型实验)
实验目的
掌握并实现Dekker互斥算法、面包店互斥算法、Peterson互斥算法和Eisenberg-Mcguire算法
二、实验原理
基于Dekker互斥算法、面包店互斥算法、Peterson互斥算法和Eisenberg-Mcguire算法的思想用Java语言编程实现
三、实验内容
本实验在Eclipse环境下,用Java语言编程实现Dekker互斥算法、面包店互斥算法、Peterson互斥算法和Eisenberg-Mcguire算法。
以下代码实现了图书借阅系统的借书问题,请在Eclipse环境下运行该程序,观察10次以上的运行结果,并分析原因。
public class Mutex{
static int x=1;
public static void main(String[] args) {
Runnable runA=new Runnable(){
public void run(){
if(x>=1){
try {
(100);
} catch (InterruptedException e) {
();
}
x--;
("in threadA the only book is borrowed!");
("in threadA x is "+x);
}
else
("in threadA no book is left and book can not be borrowed!");
}
};
Thread threadA=new Thread(runA,"threadA");
();
Runnable runB=new Runnable(){
public void run(){
if(x>=1){
try {
(100);
} catch (InterruptedException e) {
();
}
x--;
("in threadB the only book is borrowed!");
实验六 互斥算法实现(一) 来自淘豆网m.daumloan.com转载请标明出处.