-1

I have a java software running the main in a Thread with a Scanner for the user input (ie: "Input") and another Thread that outputs a String (ie: "Output") every 3 seconds. The result is this:

enter image description here

I have not time to write the input and the second Thread writes the output. Is there a way to avoid it and let the user write the input without interferences of the output?

fabio_vac
  • 584
  • 1
  • 6
  • 14
  • 1
    Your question is unclear. I believe what you want to do is block the output as soon as there is any user input detected. So your output thread will output every three seconds *unless* the user enters a character. Then, the output thread will stop outputting until after the user has pressed , at which point the output thread will start outputting again. Is that correct? If so, you might want to modify your question to indicate that specifically. Also consider adding code showing what you have tried. – kaliatech Mar 15 '16 at 12:21
  • I want to know if there is a way to let the user write the input while the second thread is writing the output. I don't want to stop the second thread. – fabio_vac Mar 15 '16 at 12:23
  • I think what are asking does not make sense in a standard I/O terminal. In a standard I/O terminal, it is not possible to write the output in a different "location" than the input. So unless you block, or at least delay writing from, the output thread, there is no way to accept input in one location while writing to another. Again, recommend you edit your question to make what you want to do clearer. – kaliatech Mar 15 '16 at 12:29
  • A way to do this in a not standard I/O terminal? – fabio_vac Mar 15 '16 at 12:31
  • 1
    Write two programs, Fxxxx, a client (consumer) and a server (producer), communicate between the client and server using a network stream (e.g. sockets, http, rmi, web sockets, whatever), start the two applications independently in seperate terminals. Use [BlockingQueues](https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/BlockingQueue.html) to buffer data. – jewelsea Mar 15 '16 at 18:25

1 Answers1

0

Per comments, I believe you are looking for a way to accept user input in one thread, while simultaneously writing output to different location in the terminal with another thread. As mentioned in comments, this does not make much sense in a simple I/O terminal. A standard terminal doesn't have the concept of "location" beyond where bytes were received/output in a stream.

Hopefully you understand now that your question is unclear and depends on what you want to do.

The most common approach for this type problem is probably to create a custom GUI terminal using Swing or JavaFX. You would have a text field accepting your input, and a separate text area displaying your output.

If wanting to stay with a text interface, then maybe look in to curses like options. Examples here: What's a good Java, curses-like, library for terminal applications?

Redirecting standard output/input with Java is not always trivial, but you'll need to ask new, more specific, questions to get more help if you go that direction.

Community
  • 1
  • 1
kaliatech
  • 17,579
  • 5
  • 72
  • 84
  • Thanks for the reply! Is there a way to redirect output to another terminal tab? Thanks – fabio_vac Mar 15 '16 at 14:31
  • It would be the best to write the output of a thread in a terminal and the output of the other thread in another terminal – fabio_vac Mar 15 '16 at 15:28