import java.util.StringTokenizer;
public class lyx extends Thread
{
static boolean mutex = false;//读锁
static boolean wmutex = false;//写锁
static boolean boo = true;//判断是读者优先或写者优先
static int rnum = 0;//读者人数
static int wwait = 0;//写者等待人数
int a;//id
String b;//R or W
double c;//起始时间
double d;//持续时间
lyx(String s,boolean boo)
//从 txt 文本中获取读者写者信息
{
StringTokenizer st=new StringTokenizer(s);
a=Integer.parseInt(st.nextToken());
b=st.nextToken();
c=Double.parseDouble(st.nextToken());
d=Double.parseDouble(st.nextToken());
this.boo=boo;
}
public void run()
{
System.out.println("线程"+a+"被创建");
try{
Thread.sleep((int)(1000*c));//请求延迟
}
catch(InterruptedException e){}
if (b.equals("R"))//如果是读者信息
{
System.out.println("线程"+a+"申请读操作");
if (boo)
while (mutex==true&&wmutex==true){}//如果是读者优先,当读锁和写锁有一个没有锁住时跳出死循环
else
while (mutex==true&&wmutex==true||wwait>0){}//写者优先时,当读锁和写锁有一个没有锁住或者没有写者等待时跳出死循环
mutex=true;
rnum++;
System.out.println("线程"+a+"开始读操作");
try{
Thread.sleep((int)(1000*d));//操作延时
}
catch(InterruptedException e){}
System.out.println("线程"+a+"结束读操作");
rnum--;//一个读操作结束,读者人数减一
if (rnum==0)
mutex=false;//读者人数为零,读锁释放
}
else if (b.equals("W"))//如果是写者信息
{
System.out.println("线程"+a+"申请写操作");
if (boo)
{
while (mutex==true){}
mutex=true;
while (wmutex==true){}
wmutex=true;
//读者优先时,读锁没有锁住时,跳出循环,把读锁锁住。写锁没有锁住时,跳出循环,锁住写锁。
}
else
{
if (mutex==true)
wwait++;
while (mutex==true){}
mutex=true;
while (wmutex==true){}
wmutex=true;
wwait--;
//写者优先时,如果读锁锁住,即有人在读或写,写者进入等待。当没有读者时,锁住读锁,没有写者时,锁住写锁,即进入写阶段,写者等待人数减一。
}
System.out.println("线程"+a+"开始写操作");
try{
Thread.sleep((int)(1000*d));//写入延时
}
catch(InterruptedException e){}
System.out.println("线程"+a+"结束写操作");
wmutex=false;
mutex=false;//写操作结束,释放读锁写锁
}
}
}
public class lyx extends Thread
{
static boolean mutex = false;//读锁
static boolean wmutex = false;//写锁
static boolean boo = true;//判断是读者优先或写者优先
static int rnum = 0;//读者人数
static int wwait = 0;//写者等待人数
int a;//id
String b;//R or W
double c;//起始时间
double d;//持续时间
lyx(String s,boolean boo)
//从 txt 文本中获取读者写者信息
{
StringTokenizer st=new StringTokenizer(s);
a=Integer.parseInt(st.nextToken());
b=st.nextToken();
c=Double.parseDouble(st.nextToken());
d=Double.parseDouble(st.nextToken());
this.boo=boo;
}
public void run()
{
System.out.println("线程"+a+"被创建");
try{
Thread.sleep((int)(1000*c));//请求延迟
}
catch(InterruptedException e){}
if (b.equals("R"))//如果是读者信息
{
System.out.println("线程"+a+"申请读操作");
if (boo)
while (mutex==true&&wmutex==true){}//如果是读者优先,当读锁和写锁有一个没有锁住时跳出死循环
else
while (mutex==true&&wmutex==true||wwait>0){}//写者优先时,当读锁和写锁有一个没有锁住或者没有写者等待时跳出死循环
mutex=true;
rnum++;
System.out.println("线程"+a+"开始读操作");
try{
Thread.sleep((int)(1000*d));//操作延时
}
catch(InterruptedException e){}
System.out.println("线程"+a+"结束读操作");
rnum--;//一个读操作结束,读者人数减一
if (rnum==0)
mutex=false;//读者人数为零,读锁释放
}
else if (b.equals("W"))//如果是写者信息
{
System.out.println("线程"+a+"申请写操作");
if (boo)
{
while (mutex==true){}
mutex=true;
while (wmutex==true){}
wmutex=true;
//读者优先时,读锁没有锁住时,跳出循环,把读锁锁住。写锁没有锁住时,跳出循环,锁住写锁。
}
else
{
if (mutex==true)
wwait++;
while (mutex==true){}
mutex=true;
while (wmutex==true){}
wmutex=true;
wwait--;
//写者优先时,如果读锁锁住,即有人在读或写,写者进入等待。当没有读者时,锁住读锁,没有写者时,锁住写锁,即进入写阶段,写者等待人数减一。
}
System.out.println("线程"+a+"开始写操作");
try{
Thread.sleep((int)(1000*d));//写入延时
}
catch(InterruptedException e){}
System.out.println("线程"+a+"结束写操作");
wmutex=false;
mutex=false;//写操作结束,释放读锁写锁
}
}
}