Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java读写器_Java_Multithreading_Synchronization - Fatal编程技术网

Java读写器

Java读写器,java,multithreading,synchronization,Java,Multithreading,Synchronization,我在编程语言的概念课上学到了一个新概念,这让我有些困惑。任何信息都很好。读写器问题如下: 这个经典问题要求读写过程同步。因此,在定义和创建读写线程之前,需要一个同步控制器。下面是一个控制器类(剩下两个方法供您实现)。为了保持编程的简单性,当您编写读写线程类(例如,三个读写器和一个写写器)时,您只需要在开始读写时象征性地打印一条消息,并在完成读写时打印另一条消息(因此,不需要创建每个线程读取或写入的实际共享内容) 这就是我所拥有的。我想我缺少了线程的一些基本功能。请注意,控制器类和方法是给定的和必

我在编程语言的概念课上学到了一个新概念,这让我有些困惑。任何信息都很好。读写器问题如下:

这个经典问题要求读写过程同步。因此,在定义和创建读写线程之前,需要一个同步控制器。下面是一个控制器类(剩下两个方法供您实现)。为了保持编程的简单性,当您编写读写线程类(例如,三个读写器和一个写写器)时,您只需要在开始读写时象征性地打印一条消息,并在完成读写时打印另一条消息(因此,不需要创建每个线程读取或写入的实际共享内容)

这就是我所拥有的。我想我缺少了线程的一些基本功能。请注意,控制器类和方法是给定的和必需的,除了startWriting()和stopWriting()必须由我实现。谢谢

class Controller {
private int activeReaders = 0;  
private boolean writerPresent = false;  

protected boolean writeCondition() {
    return activeReaders == 0 && !writerPresent;
}

protected boolean readCondition() {
    return !writerPresent;
}

protected synchronized void startRead() {
    while (!readCondition())
        try { wait(); } catch (InterruptedException ex) {}
    ++activeReaders;
}

protected synchronized void stopRead()  { 
    --activeReaders;
    notifyAll();
}

protected synchronized void startWriting(){
    writerPresent = true;
    System.out.println("Writing has begun");
}

protected synchronized void stopWriting(){
    System.out.println("Reading is now available");
    writerPresent = false;      
}

public static void main(String [] args){
    Controller c = new Controller();

    Thread tRead1   = new Thread(new Runnable() {
        @Override
        public void run(){
            c.startRead();
            System.out.println("Reader 1 has started reading");
            c.stopRead();
            System.out.println("Reader 1 has stopped reading");
        }
    });
    Thread tRead2   = new Thread(new Runnable() {
        @Override
        public void run(){
            c.startRead();
            System.out.println("Reader 2 has started reading");
            c.stopRead();
            System.out.println("Reader 2 has stopped reading");
        }
    });
    Thread tRead3   = new Thread(new Runnable() {
        @Override
        public void run(){
            c.startRead();
            System.out.println("Reader 3 has started reading");
            c.stopRead();
            System.out.println("Reader 3 has stopped reading");
        }
    });
    Thread tWrite1  = new Thread(new Runnable() {
        @Override
        public void run(){
            c.startWriting();
            c.stopWriting();

        }
    });

    tRead1.start();
    tRead2.start();
    tWrite1.start();
    try {
        tWrite1.join();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    tRead3.start();

}

}

首先,我建议您转到javadocs,阅读wait、notify和notifyall的方法定义。这是Java中基本的等待/锁定/通知系统

对于读卡器和写卡器,读卡器都应该从写卡器读取内容,如果没有可用内容,则使用写卡器上的
wait
将其暂停。如果您有多个写卡器,您可以对等待消息队列的读卡器执行相同的操作。当写卡器写入并且有更多数据供读卡器使用时r、 它应该自己(编写器)调用
notify
,唤醒一个消费者以获取新数据

并让您了解何时可以/应该使用notifyAll:


Thread.join的工作方式是对正在加入的线程调用
wait
。线程死亡时,线程调用
notifyAll
,以唤醒所有正在等待它完成的线程。

到底是什么问题?请澄清您的具体问题或添加其他详细信息,以突出显示您需要的内容。因为它当前正在写入十,很难说清楚你在问什么。你说,“我想我遗漏了一些基本的东西…”你为什么这么认为?你的程序做了什么你没有预料到的事情?另外,如果你能强调你的例子中哪些部分是你写的,哪些部分是在作业中给你的,这将是很有帮助的。我的问题是,当控制器被使用时,读的线程应该在写的时候停止发生,然后在写作结束后继续。这不是我写的方式。就基本问题而言,这是逐字提供给我的信息。包括startWriting()之后的所有内容是我写的。他的实际问题是
ReadWriteLock
实现,而不是
PipeChannel
。不过,你的答案更适合Pipe。@ControlAltDel谢谢。