I am a beginner in Java. I use Eclipse. I want to accomplish the following scenario and couldn't find how to do it:
While a Java program runs it outputs text to the console, I also want to be able to input text and process it without blocking the output by waiting for input.
Assume this:
(The code is a mockup)
//**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)
Right now, in my program I am using 1 thread for input and another one for output, but I can easily change the design. All I could find was something about server and client, I would like to keep my code in one place-package. And I currently don't know how to make a GUI with a text box for output and one for input.
Can you recommend something please?
It seems that Java allows the user to input text while another thread outputs to the console.
This is the reason why I couldn't find anything in my searches for things like "java input and output to console asynchronous". I had a problem in my input code exactly where I was asking for input and because I knew from single threaded programs that the program halts until I enter the text and press enter, I assumed that the error was thrown because the output thread was taking over the console and terminating the input thread.
Here is my code for those who search (take it as a guide, might not work if compiled):
//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);
}
}
}