java 多线程饥饿现象的问题解决方法
程序员文章站
2023-12-17 17:07:46
java 多线程饥饿现象的问题解决方法
当有线程正在读的时候,不允许写 线程写,但是允许其他的读线程进行读。有写线程正在写的时候,其他的线程不应该读写。为了防止写线程出现...
java 多线程饥饿现象的问题解决方法
当有线程正在读的时候,不允许写 线程写,但是允许其他的读线程进行读。有写线程正在写的时候,其他的线程不应该读写。为了防止写线程出现饥饿现象,当线程正在读,如果写线程请求写,那么应该禁止再来的读线程进行读。
实现代码如下:
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; } @override public void run() { // todo auto-generated method stub while(!thread.currentthread().interrupted()){ synchronized(pool){ while(pool.getwriternumber()>0 || pool.iswaittingwriten()==true)//当线程正在写或者 //有线程正在等待写,则禁止读线程继续读 { try { pool.wait(); } catch (interruptedexception e) { // todo auto-generated catch block e.printstacktrace(); } } { pool.setreadernumber(pool.getreadernumber()+1); } } system.out.println(id+" "+"is reading...."); try { thread.sleep(1000); } catch (interruptedexception e) { // todo auto-generated catch block e.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 block e.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; } @override public void run() { // todo auto-generated method stub while(!thread.currentthread().interrupted()){ synchronized(pool){ if(pool.getreadernumber()>0) pool.setwaittingwriten(true); else pool.setwaittingwriten(false); //当线程正在被读或者被写或者有线程等待读 while(pool.getwriternumber()>0 || pool.getreadernumber()>0) { try { pool.wait(); } catch (interruptedexception e) { // todo auto-generated catch block e.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 block e.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 block e.printstacktrace(); } */ } } }
main.java
package readerwriter; public class main { public static void main(string[] args) { // todo auto-generated method stub pool 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....
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
推荐阅读
-
Java常见问题之javac Hello.java找不到文件的解决方法
-
基于java时区转换夏令时的问题及解决方法
-
Java 关于eclipse导入项目发生的问题及解决方法(推荐)
-
mysql/Java服务端对emoji的支持与问题解决方法详解
-
java连不上mysql8.0问题的解决方法
-
java web中的中文乱码问题和解决方法
-
关于webwork中同名多个文件上传的解决方法问题!!!- Webwork多线程Blog
-
OY!原来是JAVA的BUG呀~~~在SwingWorker中嵌套调用SwingWorker的死锁问题解决方法。
-
environments was not found on the java.library.path 问题的解决方法
-
Android Studio无法执行Java类的main方法问题及解决方法