ReaderWriter Lock,我的代码只提供例外

时间:2013-12-04 18:13:13

标签: multithreading readerwriterlockslim readerwriterlock

我正在尝试实现Reader Writer问题..我不知道它有什么问题?
任何人都可以帮我找出原因吗?

............................................... ....

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();
        }


    }

}

它只是给了我越来越多的例外! 如果在没有线程等待的情况下调用notify()会有问题吗?

我认为不能在常量String或全局对象上调用wait()。 我见过这个here

0 个答案:

没有答案