6

I want to know how to control the console print format. My problem is that I have 2 threads, one of them constantly prints information to the console, and the other constantly asks the user to write information, but while writing if the other thread prints something, it cuts the phrase the user is writing and splits it. How do you control it?

My code of thread 1:

//Definition of variables

while (exit == false) {
    scanner = new Scanner(System.in);
    message = scanner.nextLine();
}

//Code to use user input

My code of thread 2:

//Definition of variables

while (receive.getExecutingState()) {  
    srvMsg = receive.Receive();
    System.out.println(srvMsg);
}

Console output:

No more data to show.
No more data to show.
hNo more data to show.
eNo more data to show.
llo

I want it to keep printing messages while the user write data but don't split what it's writing.

Thanks in advance!

Edit: As said by @Thomas what I want in a clearer way, is to keep the text the user is writing at the bottom of the console, and the rest being updated.

  • 1
    That's probably more related to how the commandline works than to what you have to/can do in Java. – Thomas Feb 05 '16 at 12:43
  • But as long as it's the same problem in Linux as in Windows and it needs to be resolved by programming in Java I think is related to it than just to the console @Thomas – Aníbal Muñoz Feb 05 '16 at 16:59
  • The problem is that it might not be resolvable by Java programming and the problem being the same in Linux and Windows indicates that command lines don't work that way. – Thomas Feb 05 '16 at 17:03
  • But then how other console-based applications works as I described? – Aníbal Muñoz Feb 05 '16 at 17:12
  • I'm not sure here but I'd assume that shells (such as bash, cmd etc.) as well as commandline applications (such as less, grep, etc.) are using the same display technology but are adapting it to their needs. Since you're using a shell in between the user and the Java application you'd have to find a shell that supports what you want. Besides that, is it really that necessary to display continuous output while waiting for user input? – Thomas Feb 08 '16 at 08:29
  • Yes, as the user has to enter long strings and may change its mind if sees something while writing – Aníbal Muñoz Feb 08 '16 at 23:20

1 Answers1

1

One way would be to read the character from the users one by one:

  • let the writing thread write
  • if user enters one character (or more) pause the writing thread until the user presses enter
  • when the user presses enter, resume the writing thread

The pause/resume mechanism could be done in several ways, for example by using a Sempahore with one permit that is taken by the reading thread or the writing thread in turn.

assylias
  • 321,522
  • 82
  • 660
  • 783
  • That's what I thought of as well, but the requirement seems to be to do both: "I want it to _keep printing_ messages while the user write data". – Thomas Feb 05 '16 at 12:45
  • Mmmm - it's a bit confusing: how can you write and not write at the same time?! – assylias Feb 05 '16 at 12:47
  • Yes that's what confuses me as well. I guess what he means is like the text you enter is displayed at the bottom while the top part is constantly being updated. – Thomas Feb 05 '16 at 12:51
  • Yes it's what @Thomas says – Aníbal Muñoz Feb 05 '16 at 16:55