91av视频/亚洲h视频/操亚洲美女/外国一级黄色毛片 - 国产三级三级三级三级

資源簡介

自己用Java實現(xiàn)的讀者寫者程序。 程序先從txt文本中讀取讀者寫者信息,1 R 3 5為例,1為線程序號,R表示讀者,3表示申請操作的時間(秒為單位),5表示操作持續(xù)的時間。運行后先選擇讀者/寫者優(yōu)先,之后按照文本中的描述建立線程,采用信號量機制處理互斥訪問。

資源截圖

代碼片段和文件信息

import?java.io.*;
import?java.util.*;
public?class?ReaderWriter
{
public?static?void?main(String[]?args)?
{
boolean?boo=true;
System.out.println(“讀者寫者程序?by?Jonas:“);
System.out.println(“R-讀者優(yōu)先,W-寫者優(yōu)先“);
System.out.print(“請輸入(R?or?W):“);
while?(true)
{
Scanner?reader=new?Scanner(System.in);
String?mode=reader.nextLine();
if?(mode.equals(“R“))
{
System.out.println(“讀者優(yōu)先“);
boo=true;
break;
}
else?if?(mode.equals(“W“))
{
System.out.println(“寫者優(yōu)先“);
boo=false;
break;
}
else
System.out.print(“輸入有誤請重新輸入(R?or?W):“);
}
int?i;
String?s=null;
My?m[]=new?My[10];
try{
BufferedReader?br=new?BufferedReader(new?FileReader(“Input.txt“));
for?(i=0;(s=br.readLine())!=null;i++)?
{
m[i]=new?My(sboo);
System.out.println(“線程“+m[i].a+“是“+m[i].b+“線程第“+m[i].c+“秒申請讀寫操作操作持續(xù)“+m[i].d+“秒“);
}
for?(i--;i>=0;i--)
{
m[i].start();
}
}
catch(IOException?e){}
}
}
class?My?extends?Thread
{
static?boolean?mutex=false;
static?boolean?wmutex=false;
static?boolean?boo=true;
static?int?rnum=0;
static?int?wwait=0;
int?a;
String?b;
double?c;
double?d;
My(String?sboolean?boo)
{
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+“被創(chuàng)建“);
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;
}
}
}

?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----

?????文件?????????56??2010-10-16?09:58??ReaderWriter\Input.txt

?????文件???????2210??2010-10-17?23:08??ReaderWriter\line.class

?????文件???????2208??2010-10-20?11:54??ReaderWriter\My.class

?????文件???????1926??2010-10-20?11:54??ReaderWriter\ReaderWriter.class

?????文件???????2685??2010-10-20?11:53??ReaderWriter\ReaderWriter.java

?????文件???????2685??2010-10-20?11:53??ReaderWriter\ReaderWriter.java.bak

?????目錄??????????0??2010-10-20?11:54??ReaderWriter

-----------?---------??----------?-----??----

????????????????11770????????????????????7


評論

共有 條評論