Java异步文本输入和输出

时间:2014-11-09 17:45:42

标签: java asynchronous io

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

当java程序运行时,它将文本输出到控制台,我也希望能够输入文本并处理它而不会通过等待输入来阻止输出。

假设:

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

-Thread 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 个答案:

答案 0 :(得分:3)

已解决 - 事实证明我对JAVA非常陌生。

似乎java允许用户输入文本,而另一个线程输出到控制台。

这就是我在搜索中找不到任何东西的原因,例如&#34; java输入和输出到控制台异步&#34;。我的输入代码中出现了问题,正是我要求输入的地方,因为我从单线程程序中知道程序停止,直到我输入文本并按回车键我假设错误被抛出,因为输出线程正在接管控制台并终止输入线程。

以下是我搜索的代码(将其作为指南,如果编译可能无效):

&#13;
&#13;
//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);
    }
  }

}
&#13;
&#13;
&#13;

同样非常感谢所有花时间回复的人

答案 1 :(得分:0)

我不确定你想要做什么,但如果你是新手并且你不知道怎么做guis,我会尝试一个JOptionPane

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

答案 2 :(得分: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();
}
}

不是最优雅的方式,它无法完美运作。你不得不再修补第二个Thread。有关GUI等工作方式的信息,您应该检查Swing库。谷歌搜索它应该工作得很好。

您的一些重要关键词是: JFrame,JPanel,LayoutManager,JTextArea,JTextField,JButton,ActionListener,Inner Class