📄 readerlovewriter.java
字号:
import java.io.*;
import java.util.*;
public class ReaderLoveWriter
{
public static void main(String args[])throws Exception
{
VisitData visit=new VisitData();
Scanner scan=new Scanner(new File("threads.txt"));
//文件开头要写清多少个线程
int m=scan.nextInt();//第一个读到的是线程的总数目
int a[][]=new int[m][3];//记录每个线程的信息。a[m][0]是读/写,a[m][1]是等待时间,a[m][2]是读/写时间
int h=0;
int writeThread=0,readThread=0;//读写数分别多少
int readerNum=0,writerNum=0;
while(scan.hasNext())
{
String str=scan.next();
//System.out.print(str);
if(str.equals("W")) //是写线程
{
writeThread++;
a[h][0]=0; //0 表示是 写
a[h][1]=scan.nextInt();//开始时间
a[h][2]=scan.nextInt();//持续时间
h++;
}
else //读线程
{
readThread++;
a[h][0]=0;
a[h][1]=scan.nextInt();
a[h][2]=scan.nextInt();
h++;
}
}
//System.out.println(h+""+readThread+""+writeThread);
int i=0,n=0;
Writer threadw[]=new Writer[writeThread];//建立线程
Reader threadr[]=new Reader[readThread];
for(int k=0;k<m;k++)
{
if(a[k][0]==0&&i<writeThread)
{
threadw[i]=new Writer(i+1,a[k][1],a[k][2],visit);//线程号,等待时间,读/写时间,主要方法。
i++;
}
else
{
threadr[n]=new Reader(n+1,a[k][1],a[k][2],visit);
n++;
}
}
for(int t=0;t<i;t++)
threadw[t].start();//写进程开始
for(int p=0;p<n;p++)
threadr[p].start();//读进程开始
}
}
class Reader extends Thread
{
public int waitTime,doingTime,readerNum;//等待时间,读时间,读者数
public VisitData visit;
public Reader(int readerNum,int waitTime,int doingTime,VisitData visit )
{
super();
this.waitTime=waitTime;//等待时间
this.doingTime=doingTime;//读时间
this.readerNum=readerNum;//读者号
this.visit=visit;
}
public void run()
{
while(true){
try
{
Thread.sleep(waitTime*1000);
}
catch(Exception e){e.printStackTrace();}
System.out.println("-----读者"+readerNum+"----请求读^0^\n");
visit.startRead(readerNum);//开始读
System.out.println("-----读者"+readerNum+"----正在读。。。。^0^\n");
visit.doing(doingTime);//读书中。。
visit.endRead(readerNum); //读完
}
}
}
class Writer extends Thread
{
public int waitTime,doingTime,writerNum;
public VisitData visit;
public Writer(int writerNum,int waitTime,int doingTime,VisitData visit )
{
super();
this.waitTime=waitTime;//等待时间
this.doingTime=doingTime;//写时间
this.writerNum=writerNum;//写者号
this.visit=visit;
}
public void run()
{
while(true){
try
{
Thread.sleep((waitTime+1)*1000);//一直等到这个线程开始的时间
}
catch(Exception e){e.printStackTrace();}
System.out.println("-----写者"+writerNum+"----请求写^0^\n");
visit.startWrite(writerNum);//开始写
System.out.println("-----写者"+writerNum+"----正在写。。。。^0^\n");
visit.doing(doingTime);//正在写
visit.endWrite(writerNum);//结束写
}
}
}
class VisitData //访问数据
{
private int readerCount;
private int writerCount;
private boolean beReading;//是否有人在读
private boolean beWriting;//是否有人在写
public VisitData() //初始化
{
super();
readerCount=0;
writerCount=0;
beReading=false;
beWriting=false;
}
public void doing(int sleepTime )//用来表示正在读或写
{
try{
Thread.sleep((sleepTime+1)*1000);
}catch(Exception e){e.printStackTrace();}
}
public synchronized void startRead(int reader)
{
while(writerCount>0)
{
try{
System.out.println("-----读者"+reader+"----等待中。。。。^0^\n");
wait();
}catch(Exception e){e.printStackTrace();}
}
readerCount++;
if(readerCount>=1)
{
beReading=true;//表示有人在读
}
}
public synchronized void endRead(int reader)
{
--readerCount;
if(readerCount==0)
{
beReading=false;//表示没人在读
}
notifyAll();
System.out.println("-----读者"+reader+"----读结束^0^\n");
}
public synchronized void startWrite(int writer)
{
++writerCount;
while(beReading==true||beWriting==true)//当有读者或是写者在读或是写的话
{
try
{
System.out.println("-----写者"+writer+"----等待中。。。。^0^\n");
wait();
}catch(Exception e){System.out.println(e.toString());}
}
beWriting =true;
}
public synchronized void endWrite(int writer)
{
--writerCount;
beWriting=false;
System.out.println("-----写者"+writer+"----写结束^0^\n");
notifyAll();//通知其他人
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -