writing data to console at the same time printinf information in java
Asked Answered
V

1

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.

Varix answered 5/2, 2016 at 12:36 Comment(6)
That's probably more related to how the commandline works than to what you have to/can do in Java.Weevil
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 @WeevilLazaro
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.Weevil
But then how other console-based applications works as I described?Lazaro
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?Weevil
Yes, as the user has to enter long strings and may change its mind if sees something while writingLazaro
Y
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.

Yeoman answered 5/2, 2016 at 12:44 Comment(4)
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".Weevil
Mmmm - it's a bit confusing: how can you write and not write at the same time?!Yeoman
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.Weevil
Yes it's what @Weevil saysLazaro

© 2022 - 2024 — McMap. All rights reserved.