Java asynchronous text input and output -
i must beginner in java. utilize eclipse. want accomplish next scenario , couldn't find how it:
while java programme runs outputs text console, want able input text , process without blocking output waiting input.
assume this:
-thread 1 outputs number console every second
-thread 2 listens input
(the code mockup)
//**thread 1:** int incrementby = 0; (int = 0; < 1000; i++) { = + incrementby; //pause 1 seconds seek { thread.sleep(1000); } grab (interruptedexception e) { system.out.println("text output interupted"); } //print text system.out.println(i); } //**thread 2:** string myincrement = system.console().readline(); (now process input , alter incrementby var in thread 1)
right in programme using 1 thread input , 1 output. can alter design. find server , client, maintain code in 1 place-package. , don't know how create gui text box output , 1 input.
can recommend please?
solved - turns out very new java.
it seems java allows user input text while thread outputs console.
this reason why couldn't find in searches things "java input , output console asynchronous". had problem in input code asking input , because knew single threaded programs programme halts until come in text , press come in assumed error thrown because output thread taking on console , terminating input thread.
here code search (take guide, might not work if compiled):
class="snippet-code-js lang-js prettyprint-override">//main app public class textinpuoutputmanager { //here create 2 threads (objects implement runnable interface) static textinputobject ti; static textoutputobject to; public static void main(string[] args) { //we instantiate objects ti = new textinputobject(); = new textoutputobject(); //we phone call start method start threads input , output ti.start(); to.start(); } } //textinputobject class public class textinputobject implements runnable { //method gets called when object instantiated public textinputobject() { system.out.println("created textinputobject"); } //create thread object , check if it's not created static thread thread; //this method gets called main public void start() { if (thread == null) { thread = new thread(this); thread.start(); } } //this method gets called thread.start(); above @ override public void run() { system.out.println("text input thread created , runs"); readtextfromconsole(); } scanner inputreader = new scanner(system.in); //check input time - not halt programme 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 gets called when object 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 , runs"); //make output text every 4 seconds test if can input text while it's used output (int = 0; < 100; i++) { //pause 4 seconds seek { thread.sleep(4000); } grab (interruptedexception e) { system.out.println("text output interupted"); } //print console system.out.println(i); } } }
also big give thanks of took time respond
java asynchronous io
No comments:
Post a Comment