簡體   English   中英

Java異步文本輸入和輸出

[英]Java asynchronous text input and output

我必須說我是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。

你能推薦點什么嗎?

已解決-事實證明我是JAVA的新手。

似乎java允許用戶輸入文本,而另一個線程輸出到控制台。

這就是為什么我在搜索“ java輸入和輸出到控制台異步”之類的東西時找不到任何東西的原因。 我的輸入代碼中有一個問題,確切的是我要輸入的位置,因為我從單線程程序知道該程序會暫停,直到我輸入文本並按Enter鍵為止。控制台並終止輸入線程。

這是我的搜索代碼(作為指導,如果經過編譯,可能無法正常工作):

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

也非常感謝您抽出寶貴的時間回復大家

我不確定您到底想做什么,但是如果您是新手並且不知道如何制作GUI,我會嘗試使用JOptionPane

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

您可以創建兩個內部類,並在兩者中實現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,內部類

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM