I've been tryna find an answer to this for a while but I'm a newbie with little hope left lol
In Java, I have two threads right now:
(main class) Thread A has a while loop listening to commands with Scanner.nextLine()
Thread B prints out "test" every second
When I start both threads, I can't properly type a command without it getting interrupted by Thread B's printing
Output:
test
test
commantest
dctest
ommandtest
test
test
How do I prevent this so I can type commands without any interruptions? I'm open to using ANSI Escape codes and JCurses (or any libraries reallyy) :)
Thanks!
There is no way of preventing this. The two threads are essentially using the same console, which for you looks messed up, but is in fact working exactly as it should. Note that both reading and writing should still work, despite the text on your console being garbled.
If you want to use the console for interacting with the user, you need to ensure that no other threads are using it at the same time. Depending on your use case you could for example send the other prints to a file, named pipe etc. Alternatively, if you are designing the console application with curses, you need to properly design it for multithreading (which, again, boils down to having a single thread dealing with the console, but in a way that keeps things on separate parts of the screen).