Unterste Zeile der Java Konsole für Input reservieren?
Hallo!
Ich Arbeite grade an einem Java Programm welches Input über System.console().readLine() nimmt und dann in einem neuen Thread die Aufgabe startet, welche auf der Konsole loggt. Das Problem: Wenn ich am tippen bin für einen weiteren Befehl wird dieser zwar korrekt verarbeitet, aber er wird visuell auf der Konsole mit den Logs gemischt (siehe Bild)
Das Output sollte “Temperatur von Thermometer ID:…” sein, aber wie man beim zweiten Thermometer sehen kann, wurde ein Input von “ew” (ich habe nur auf meine Tastatur geklopft) mit dem Log gemischt. Es sollte allerdings so sein, wie z.B. bei Minecraft Servern, wo die Unterste Zeile der Konsole IMMER für Eingaben reserviert ist.
Danke!
“There can only be one”
…this applies not only to the Highlander, but also to writing threads.
Multithreading requires a high organizational effort so that there are no collisions. Your output is a collision…
Your log thread is munter in the system.Console and this has only one write cursor which (if not explicitly controlled by positioning commands) just move on with each character horizotal and change to the next line in a line feed. That’s all.
Now you have a 2nd parallel thread that does exactly the same. If you enter something, it will be “echot” to the current position of the cursor.
Both threads don’t know anything about each other, so they just send their characters to the Console and it’s totally Wumpe where the characters come from. It’s all coming on the screen. Your entered letters have just landed in front, owed more or less to the fact that you tap more slowly than the log thread.
That’s another thing. Such “consoles” are GUIs who look like “console” or real console applications which are designed so that they try to read characters individually in continuous loop and write to a fixed position. In the “free time” between the read operations, the loop takes care of writing buffered data from other threads/processes. This ensures that only one takes care of writing (and the cursor positioning).
Parallel processes put their “writing wishes” in a “letter box” (FIFO buffer)
If I would write you such a thing, it would be a “Fred Feuerstein” number, and we do not want to go towards Stone Age.
The simpler variant would be an event-controlled GUI (if you want to make the background black and the font white… then looks like Console, but has the full functionality of a GUI. This means that you only have to take care of the layout and inform the text field responsible for the output via event that has new data and where they are to be collected…
or
I had to try ChatGPT. Try this.
In this example the thread reads
the input lines via a
during the thread
writes the output lines to the console. Since these threads are executed separately, the input and output requests should be displayed in separate rows.
In C# you can easily lock the console with curl() and thus allow the user’s input and stop various logging tools from not mixing. Maybe ChatGPT’s solution will continue?
My problem still exists