Multithreading ReaderWriter锁,我的代码只给出异常
我正试图解决读写器问题。。我不知道它怎么了?Multithreading ReaderWriter锁,我的代码只给出异常,multithreading,readerwriterlockslim,readerwriterlock,Multithreading,Readerwriterlockslim,Readerwriterlock,我正试图解决读写器问题。。我不知道它怎么了? 有人能帮我弄清楚原因吗 package readerWriterController; import java.util.ArrayList; import java.util.Scanner; public class Main { public class ReaderThread extends Thread{ String threadName; public ReaderThread(String threadNam
有人能帮我弄清楚原因吗
package readerWriterController;
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public class ReaderThread extends Thread{
String threadName;
public ReaderThread(String threadName){
super(threadName);
}
public void run(){
System.out.println(getName() + " started reading!");
p();
System.out.println(getName() + " Read : " + Main.buffer);
System.out.println(getName() + " finished reading!");
v();
}
private void p() {
semaphore++;
if(isBlocked){ // if writing
try{
System.out.println(getName() + " is blocked!!");
wait();
}catch (InterruptedException e) {
e.printStackTrace();
}
}
}
private void v() {
--semaphore;
if(semaphore == 0 )notify();
}
}
public class WriterThread extends Thread{
private String text;
public WriterThread(String threadName, String text){
super(threadName);
this.text = text;
}
public void run(){
System.out.println(getName() +" start writing");
if(!isBlocked && semaphore == 0){
isBlocked = true;
synchronized (buffer) {
System.out.println(getName() +" Writing : " + Main.buffer + " " + text);
Main.buffer += text;
System.out.println(getName() +" finished writing!!" );
notify();
}
isBlocked=false;
}else if(isBlocked || semaphore != 0){ // just else !
System.out.println(getName() + " is blocked!");
try{
wait();
}catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public static String buffer;
public static int readerCounter = 0;
public static int semaphore ;
private volatile boolean isBlocked=false;
//public boolean semaphore ;
public static void main(String[] args) {
//System.out.println(readerCounter);
Scanner input = new Scanner(System.in);
System.out.println("Enter the buffer:");
buffer = input.next();
int Nr, Nw;
System.out.println("Enter the number of readers");
Nr = input.nextInt();
semaphore = 0;
System.out.println("Enter the number of writers");
Nw = input.nextInt();
// for the reader threads ...
String name;
ArrayList<Thread> list = new ArrayList<Thread>();
for(int i = 0 ; i < Nr ; i++){
// get reader info.
System.out.println("Enter reader "+ (i+1) + " name :");
name = input.next();
// pass to thread!!
list.add(new Main().new ReaderThread(name));
}
// for the writer thread ...
String text;
for(int i = 0 ; i < Nw ; i++){
// get reader info.
System.out.println("Enter writer "+ (i+1) + " name :");
name = input.next();
System.out.println("Enter the text to be written by this writer:");
text = input.next();
// pass to thread!!
list.add(new Main().new WriterThread(name, text));
}
for(Thread t : list){
t.start();
}
}
}
包读写控制器;
导入java.util.ArrayList;
导入java.util.Scanner;
公共班机{
公共类ReaderThread扩展线程{
字符串threadName;
公共读取器线程(字符串threadName){
超级(线程名称);
}
公开募捐{
System.out.println(getName()+“开始读取!”);
p();
System.out.println(getName()+“读取:”+Main.buffer);
System.out.println(getName()+“完成读取!”);
v();
}
私有void p(){
信号量++;
if(isBlocked){//if写入
试一试{
System.out.println(getName()+“被阻止!!”;
等待();
}捕捉(中断异常e){
e、 printStackTrace();
}
}
}
私人空间v(){
--信号量;
if(信号量==0)notify();
}
}
公共类WriterThread扩展线程{
私有字符串文本;
公共WriterThread(字符串threadName,字符串文本){
超级(线程名称);
this.text=文本;
}
公开募捐{
System.out.println(getName()+“开始写入”);
如果(!isBlocked&&semaphore==0){
isBlocked=true;
已同步(缓冲区){
System.out.println(getName()+“写入:”+Main.buffer+“+text);
Main.buffer+=文本;
System.out.println(getName()+“已完成写入!!”;
通知();
}
isBlocked=false;
}else如果(isBlocked | | semaphore!=0){//只是else!
System.out.println(getName()+“被阻止!”);
试一试{
等待();
}捕捉(中断异常e){
e、 printStackTrace();
}
}
}
}
公共静态字符串缓冲区;
公共静态整数读取器计数器=0;
公共静态int信号量;
private volatile boolean isBlocked=false;
//公共布尔信号量;
公共静态void main(字符串[]args){
//系统输出打印项次(读计数器);
扫描仪输入=新扫描仪(System.in);
System.out.println(“输入缓冲区:”);
buffer=input.next();
北区,西北部;
System.out.println(“输入读卡器的数量”);
Nr=input.nextInt();
信号量=0;
System.out.println(“输入写入程序数”);
Nw=input.nextInt();
//对于读者线程。。。
字符串名;
ArrayList=新建ArrayList();
对于(int i=0;i
它只是给了我越来越多的例外!!!
如果在没有线程等待时调用notify()是否有问题
我认为不能对常量字符串或全局对象调用wait()。!
我见过这个 您应该查看异常并用谷歌搜索它。例如,这可能会给你一个非法的MonitorStateException。我已经做到了。。但是我没有得到任何有价值的东西。。或者也许我不明白我得到了什么好!