JAVA多线程实现读者写者问题
来源:互联网 发布:赢在中国知乎 编辑:程序博客网 时间:2024/04/30 17:56
当有线程正在读的时候,不允许写 线程写,但是允许其他的读线程进行读。有写线程正在写的时候,其他的线程不应该读写。为了防止写线程出现饥饿现象,当线程正在读,如果写线程请求写,那么应该禁止再来的读线程进行读。
File.java
package readerWriter;public class File {private String name;public File(String name){this.name=name;}}
Pool.java
package readerWriter;public class Pool {private int readerNumber=0;private int writerNumber=0;private boolean waittingWriten;public boolean isWaittingWriten() {return waittingWriten;}public void setWaittingWriten(boolean waittingWriten) {this.waittingWriten = waittingWriten;}public File getFile() {return file;}public void setFile(File file) {this.file = file;}File file;public Pool(File file){this.file=file;}public int getReaderNumber() {return readerNumber;}public void setReaderNumber(int readerNumber) {this.readerNumber = readerNumber;}public int getWriterNumber() {return writerNumber;}public void setWriterNumber(int writerNumber) {this.writerNumber = writerNumber;}}
Reader.java
package readerWriter;public class Reader implements Runnable{ private String id;private Pool pool; public Reader(String id,Pool pool) { this.id=id; this.pool=pool; } @Overridepublic void run(){// TODO Auto-generated method stubwhile(!Thread.currentThread().interrupted()){synchronized(pool){while(pool.getWriterNumber()>0 || pool.isWaittingWriten()==true)//当线程正在写或者 //有线程正在等待写,则禁止读线程继续读 {try {pool.wait();} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}{pool.setReaderNumber(pool.getReaderNumber()+1); }} System.out.println(id+" "+"is reading...."); try {Thread.sleep(1000);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}synchronized(pool){ pool.setReaderNumber(pool.getReaderNumber()-1); System.out.println(id+" "+"is existing the reader...."); if(pool.getReaderNumber()==0) pool.notifyAll(); } try {Thread.sleep(1000);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}// pool.notifyAll(); } }}
Writer.java
package readerWriter;public class Writer implements Runnable{ private Pool pool; String id; public Writer(String id,Pool pool) { this.id=id; this.pool=pool; }@Overridepublic void run() {// TODO Auto-generated method stubwhile(!Thread.currentThread().interrupted()){synchronized(pool){if(pool.getReaderNumber()>0)pool.setWaittingWriten(true);elsepool.setWaittingWriten(false);//当线程正在被读或者被写或者有线程等待读while(pool.getWriterNumber()>0 || pool.getReaderNumber()>0) {try {pool.wait(); } catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}pool.setWaittingWriten(false); //这个策略还算公平{pool.setWriterNumber(pool.getWriterNumber()+1); }} System.out.println(id+" "+"is writing...."); try {Thread.sleep(1000);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();} //synchronized(pool){ pool.setWriterNumber(pool.getWriterNumber()-1); System.out.println(id+" "+"is existing the writer...."); pool.notifyAll(); } /* try {Thread.sleep(1000);//System.out.println("writer sleeping over");} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();} */ }}}
Main.java
package readerWriter;public class Main {public static void main(String[] args) {// TODO Auto-generated method stubPool pool=new Pool(new File("dd file"));for(int i=0;i<2;i++){ Thread writer=new Thread(new Writer("writer "+i,pool)); writer.start();}for(int i=0;i<5;i++){Thread reader=new Thread(new Reader("reader "+i,pool));reader.start();}}}
程序部分运行结果如下:
writer 0 is writing....writer 0 is existing the writer....writer 0 is writing....writer 0 is existing the writer....writer 0 is writing....writer 0 is existing the writer....writer 0 is writing....writer 0 is existing the writer....writer 1 is writing....writer 1 is existing the writer....writer 1 is writing....writer 1 is existing the writer....writer 1 is writing....writer 1 is existing the writer....writer 1 is writing....writer 1 is existing the writer....writer 0 is writing....writer 0 is existing the writer....writer 0 is writing....writer 0 is existing the writer....reader 0 is reading....reader 0 is existing the reader....writer 1 is writing....writer 1 is existing the writer....writer 1 is writing....writer 1 is existing the writer....writer 1 is writing....writer 1 is existing the writer....writer 1 is writing....writer 1 is existing the writer....writer 1 is writing....writer 1 is existing the writer....writer 0 is writing....writer 0 is existing the writer....writer 0 is writing....writer 0 is existing the writer....writer 0 is writing....writer 0 is existing the writer....writer 0 is writing....writer 0 is existing the writer....reader 3 is reading....reader 2 is reading....reader 4 is reading....reader 1 is reading....reader 0 is reading....reader 3 is existing the reader....reader 1 is existing the reader....reader 0 is existing the reader....reader 4 is existing the reader....reader 2 is existing the reader....writer 0 is writing....writer 0 is existing the writer....writer 0 is writing....writer 0 is existing the writer....writer 1 is writing....writer 1 is existing the writer....reader 2 is reading....reader 2 is existing the reader....writer 0 is writing....writer 0 is existing the writer....writer 0 is writing....writer 0 is existing the writer....writer 0 is writing....
0 0
- JAVA多线程实现读者写者问题
- Java 多线程读者写者问题
- 读者写者问题(Java多线程)
- java实现读者-写者问题
- 多线程---读者写者问题
- java 读者写者问题
- Java实现生产者消费者问题与读者写者问题
- Java实现生产者消费者问题与读者写者问题
- 多线程练习----读者写者问题
- 多线程的读者写者问题
- 多线程11:读者写者问题
- 多线程_-读者写者问题
- <MFC多线程> 读者写者问题
- 【多线程】(十)读者写者问题
- 秒杀多线程-读者写者问题
- linux C++ 多线程 读者写者实现
- 用JAVA实现的第二类读者写者问题
- 多线程学习系列三 多线程实现读者和写者问题
- android常用布局LinearLayout
- ubantu下面的nfs安装和配置
- 在raspberry pi(树莓派)上连接VPN
- 单态设计模式
- java web应用在tomcat下servlet api包冲突问题
- JAVA多线程实现读者写者问题
- 1583个mms电台列表
- Android:防止过快点击造成多次事件
- oracle适配器连接不上解决方案
- HDU - 1702 ACboy needs your help again!
- AIX6 arg list too long
- 《Redis设计与实现》学习笔记-Sentinel(哨岗、哨兵)
- 闪讯在windows7使用免费wifi!!
- hdoj 1237 简单计算器