Java异步文本输入和输出

3
我必须说我是Java的初学者。 我使用Eclipse。 我想完成以下场景,但无法找到如何做到:

当Java程序运行时,它会将文本输出到控制台,我还希望能够输入文本并在不阻塞输出的情况下处理它。

假设这样:

-线程1每秒向控制台输出一个数字

-线程2监听输入

(该代码是一个模拟)

//**Thread 1:**

int incrementBy = 0;

for (int i = 0; i < 1000; i++) {

    i = i + incrementBy;

    //Pause for 1 seconds
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
        System.out.println("TEXT OUTPUT INTERUPTED");
    }
    //Print text
    System.out.println(i);
}



//**Thread 2:**
String myIncrement = System.console().readLine();

(Now process the input and change the incrementBy var in Thread 1)

目前在我的程序中,我正在使用1个线程用于输入,另一个用于输出。但我可以轻松更改设计。 我发现的所有内容都与服务器和客户端有关,我希望将我的代码放在一个地方-包中。 我目前不知道如何制作具有输出文本框和输入文本框的GUI。

你能推荐一些东西吗?

3个回答

6
解决了 - 我非常非常新手Java。
似乎Java允许用户在另一个线程输出到控制台时输入文本。
这就是为什么我无法在搜索中找到任何关于“Java输入和输出到控制台异步”的信息。我的输入代码有问题,正是在我要求输入的地方,因为我从单线程程序中知道程序会暂停,直到我输入文本并按回车键,所以我假设错误是由于输出线程占用控制台并终止输入线程而引发的。
以下是我的代码,供那些搜索的人参考(仅供参考,如果编译可能不起作用):

//Main app
public class textInpuOutputManager {

  //here we create the two threads (objects that implement the runnable interface)
  static TextInputObject ti;
  static TextOutputObject to;

  public static void main(String[] args) {
    //we instantiate the objects
    ti = new TextInputObject();
    to = new TextOutputObject();
    //we call the start method to start the threads for input and output
    ti.start();
    to.start();
  }

}


//TextInputObject class
public class TextInputObject implements Runnable {

  //Method that gets called when the object is instantiated
  public TextInputObject() {
    System.out.println("Created TextInputObject");
  }

  //create a thread object and check if it's not already created
  static Thread thread;

  //This method gets called from the main
  public void start() {
    if (thread == null) {
      thread = new Thread(this);
      thread.start();
    }
  }

  //this method gets called by the thread.start(); from above
  @
  Override
  public void run() {
    System.out.println("Text input thread created and now it runs");

    readTextFromConsole();
  }

  Scanner inputReader = new Scanner(System.in);

  //check for input all the time - THIS WILL NOT HALT THE PROGRAM
  public void readTextFromConsole() {
    System.out.println("Enter something:");
    String myinput = inputReader.nextLine();
    System.out.println("You Entered: " + myinput);
    readTextFromConsole();
  }

}


//TextOutputObject
public class TextOutputObject implements Runnable {

  //Method that gets called when the object is instantiated
  public TextOutputObject() {
    System.out.println("Created TextOutputObject");
  }

  static Thread thread;

  public void start() {
    if (thread == null) {
      thread = new Thread(this);
      thread.start();
    }
  }

  @
  Override
  public void run() {
    System.out.println("Text output thread created and now it runs");

    //Make it output text every 4 seconds to test if you can input text while it's used for output
    for (int i = 0; i < 100; i++) {
      //Pause for 4 seconds
      try {
        Thread.sleep(4000);
      } catch (InterruptedException e) {
        System.out.println("TEXT OUTPUT INTERUPTED");
      }
      //Print i to console
      System.out.println(i);
    }
  }

}

同时非常感谢所有抽出时间回复的朋友们。


0

我不确定你想做什么,但如果你是新手并且不知道如何制作GUI界面,我建议尝试使用JOptionPane。

 String input = JOptionPane.showInputDialog("User input is returned as a string; use Integer.parseInt(input) to retrieve an integer from this method");

0
你可以创建两个内部类并在两个内部类中实现Runnable接口。
import java.util.Scanner;

public class Test{

private Thread t1;
private Thread t2;    

public static void main(String[] args){
    new Test();
}

private class TOne implements Runnable{
    public void run(){
        int incrementBy = 0;

        for (int i = 0; i < 1000; i++) {

            i = i + incrementBy;

            //Pause for 1 seconds
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                System.out.println("TEXT OUTPUT INTERUPTED");
            }
            //Print text
            System.out.println(i);
        }
    }
}

private class TTwo implements Runnable{
    public void run(){//Code for Thread 2
        try{
            Scanner scr = new Scanner(System.in);
            System.out.println(scr.next());
        }catch(Exception ex){
            ex.printStackTrace();
        }
    }
}
public Test(){
    t1 = new Thread(new TOne());
    t1.run();
    t2 = new Thread(new TTwo());
    t2.run();
}
}

这不是最优雅的方式,也不是完美无缺的。你需要再调整一下第二个线程。如果想了解GUI等工作原理,你应该查看Swing库。谷歌搜索应该可以很好地解决。

对于你来说,一些重要的关键词可能是: JFrame、JPanel、LayoutManager、JTextArea、JTextField、JButton、ActionListener、内部类


谢谢user3046986,你的回答几乎和我的方法一样,我的问题是由于糟糕的编码而抛出的异常,恰好在我要求输入的地方,因为我的过去经验,我跳到了控制台不允许它的结论。你的回答让我更仔细地查看了我的代码。还要感谢你提供的关键词,我一定会去查看的。 - hi-X BOty

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接