Results 1 to 5 of 5
  1. #1
    racnil is offline Member
    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0

    Default [SOLVED]Multi-threading Problem with Console Input and Output

    Hello everyone,

    My question in a short (but maybe in a wrong access) form:

    Is there any methode to get the characters of an UNFINISHED user input? "Unfinished" means the user has inputed something but has not pressed "Enter" key yet.


    My question in a long and detailed form:

    I want to implement a server program, that has threads for each connection. Each threads should report its state via a console output --- such as a new connection has been established; meanwhile the main thread should be able to control the connections via console input --- such as close a connection, or send some messages to an specified clients.

    I tried console input and output in a "normal way", but if a "connection thread output" comes after the main thread shows the prompt of the control command, everything is messed up like this:

    command> Connection reports goes here
    reports goes here
    and here
    and here
    _ <--- finally here is the cursor of new control command.

    I've already tried "\b", before connection thread outputs any reports, output "\b"s back to the start of a line and then output the reports and finally append a "command> " after the reports. It also has problems. If the server user input something after the command prompt, but has not finished yet, while a new report from the connection thread comes. It looks like:

    commConnection reports goes here
    reports goes here
    and here
    and here
    command> _

    because I have outputed 9 "\b"s to overlap "command> "; however, if user has already typed something like "command> clos", then the last 9 chars will be overlaped, so "comm" remains.

    Is there any way to solve the problem? Thanks a lot! And sorry for my English if I typed something wrong, because English is not my mother tongue.

    racnil
    Last edited by racnil; 11-27-2012 at 11:08 AM. Reason: Solved problem

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,817
    Rep Power
    19

    Default Re: Multi-threading Problem with Console Input and Output

    Do you have multiple threads writing to the console?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    racnil is offline Member
    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0

    Default Re: Multi-threading Problem with Console Input and Output

    Quote Originally Posted by Tolls View Post
    Do you have multiple threads writing to the console?
    Hello Tolls,
    Thanks for your reply.
    Yes, there are threads writing to the console, and one of them even reads from the console. But how can we synchronize among the threads?

    Zihan
    Last edited by racnil; 11-27-2012 at 10:08 AM.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,817
    Rep Power
    19

    Default Re: Multi-threading Problem with Console Input and Output

    I would recommend not doing that.

    If you need to display stuff coming in from one thing, while listening for use rinput on another, then this should probably be a Swing/JavaFX GUI, so you can have different components.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    racnil is offline Member
    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0

    Default Re: Multi-threading Problem with Console Input and Output

    Thanks Tolls!
    I am also considering to convert the UI to swing instead of console. It's practical.

Similar Threads

  1. standard input / output in console
    By chipp in forum New To Java
    Replies: 10
    Last Post: 05-11-2012, 07:45 PM
  2. Multi-Threading
    By TonyDaniels in forum New To Java
    Replies: 4
    Last Post: 03-09-2012, 06:16 PM
  3. Multi-Threading
    By TonyDaniels in forum Advanced Java
    Replies: 1
    Last Post: 03-09-2012, 05:52 PM
  4. multi-threading problem
    By imorio in forum New To Java
    Replies: 1
    Last Post: 04-25-2011, 06:52 PM
  5. Problem in Multi threading.
    By Chetans in forum Advanced Java
    Replies: 3
    Last Post: 03-23-2010, 04:42 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •