如何在 Java 中其他方法完成时暂停控制台 intpu
How do I pause console intpu while other method finish in Java
我在 Java 中有以下任务:
用户正在向控制台写入消息。我正在阅读此输入并将其处理为相关对象(书籍)。当处理的书籍数量等于 30 时,我必须暂停控制台输入,记录到目前为止已处理书籍的报告,然后继续从控制台接收消息。 (既然是控制台应用,假设是单线程应用)
我是并发编程的新手,但根据我的阅读,我需要使用 wait()
和 notify()
方法。
到目前为止,我的应用程序结构如下:
InputReader.java
- main class 从控制台读取输入并转换为相关对象
BookReader.java
- class,其中最重要的方法是处理单本书(同时记录到目前为止处理的书籍数量)。让我们称之为 public static store(Book book)
。
所以在 InputReader.java
中读取单个控制台行后,我将它处理成一个 Book
对象,然后我调用 BookReader.keep(book)
(因为 BookReader.java
calss 中的 store
方法是静态的)。
问题(或我遇到的问题)是如何在阅读第 30 本书后暂停控制台,然后打印输出。
从我读过的内容来看,我应该在我的 store(Book book)
方法中使用 wait()
并在将执行报告的方法中使用 notify()
。
由于 store(Book book)
是一种 static
方法,我读过我应该在 BookStore.java
class 中使用 private static final Object lock = new Object();
?所以目前我的结构如下所示:
protected static synchronized void reader(Book book) throws InterruptedException {
counter++;
//some method which will keep my books into a local datastructure
if(counter == 30) {
LOGGER.info("The console should stop accept new input now");
synchronized(lock){
lock.wait();
}
// my main question: do I need to call printReport() here? If not here, then where?
report();
}
}
和打印报告后的 report()
方法应该使控制台可用于阅读新消息:
private static synchronized void report() {
synchronized(lock){
System.out.printline("print some report here")
}
// I guess after the report is printed, I should use notifyAll() and that will make the console again available for reading an input?
lock.notifyAll();
}
}
请记住,因为我的 store(Book book)
是静态的,所以我使用 static Object lock = new Object();
作为 BookReader.java
class 的对象。
我的主要问题:我是否在正确的位置调用了 report()
方法?如果不在那里那么在哪里?
我的想法是制作一个"work-around"并将"report time"期间所有传输的内容保存在一个类似队列的集合中,然后在您报告状态时读取队列中的书籍。 Mabye 那是您问题的另一种解决方案。
以下程序从控制台读取 30 行,写入报告并重复
import java.util.Scanner;
public class ScannerTest {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (true) {
String report = "";
for (int i = 0; i < 30; i++) {
report += sc.nextLine()+" ";
}
System.out.println(report);
}
}
}
我在 Java 中有以下任务: 用户正在向控制台写入消息。我正在阅读此输入并将其处理为相关对象(书籍)。当处理的书籍数量等于 30 时,我必须暂停控制台输入,记录到目前为止已处理书籍的报告,然后继续从控制台接收消息。 (既然是控制台应用,假设是单线程应用)
我是并发编程的新手,但根据我的阅读,我需要使用 wait()
和 notify()
方法。
到目前为止,我的应用程序结构如下:
InputReader.java
- main class 从控制台读取输入并转换为相关对象
BookReader.java
- class,其中最重要的方法是处理单本书(同时记录到目前为止处理的书籍数量)。让我们称之为 public static store(Book book)
。
所以在 InputReader.java
中读取单个控制台行后,我将它处理成一个 Book
对象,然后我调用 BookReader.keep(book)
(因为 BookReader.java
calss 中的 store
方法是静态的)。
问题(或我遇到的问题)是如何在阅读第 30 本书后暂停控制台,然后打印输出。
从我读过的内容来看,我应该在我的 store(Book book)
方法中使用 wait()
并在将执行报告的方法中使用 notify()
。
由于 store(Book book)
是一种 static
方法,我读过我应该在 BookStore.java
class 中使用 private static final Object lock = new Object();
?所以目前我的结构如下所示:
protected static synchronized void reader(Book book) throws InterruptedException {
counter++;
//some method which will keep my books into a local datastructure
if(counter == 30) {
LOGGER.info("The console should stop accept new input now");
synchronized(lock){
lock.wait();
}
// my main question: do I need to call printReport() here? If not here, then where?
report();
}
}
和打印报告后的 report()
方法应该使控制台可用于阅读新消息:
private static synchronized void report() {
synchronized(lock){
System.out.printline("print some report here")
}
// I guess after the report is printed, I should use notifyAll() and that will make the console again available for reading an input?
lock.notifyAll();
}
}
请记住,因为我的 store(Book book)
是静态的,所以我使用 static Object lock = new Object();
作为 BookReader.java
class 的对象。
我的主要问题:我是否在正确的位置调用了 report()
方法?如果不在那里那么在哪里?
我的想法是制作一个"work-around"并将"report time"期间所有传输的内容保存在一个类似队列的集合中,然后在您报告状态时读取队列中的书籍。 Mabye 那是您问题的另一种解决方案。
以下程序从控制台读取 30 行,写入报告并重复
import java.util.Scanner;
public class ScannerTest {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (true) {
String report = "";
for (int i = 0; i < 30; i++) {
report += sc.nextLine()+" ";
}
System.out.println(report);
}
}
}