Results 1 to 3 of 3
  1. #1
    johnmerlino is offline Member
    Join Date
    May 2014
    Posts
    54
    Rep Power
    0

    Default Losing control of the console

    I have a multithreaded java program. I have a few worker threads which process data over a udp server. Then I have another thread for console input. The problem is when there is an error with one of the worker threads, and it prints to the console, my console thread loses "focus", that is, the while loop seems to no longer run. Here is the console thread:

    Java Code:
    	public void run() {
    		Console c = System.console();
    		if (c == null) {
    		    Logger.writeError("No console.");
    		    System.exit(1);
    		}
    		
    		while(true){
    			String login = c.readLine(">>>: ");
    			System.out.println("You entered: " + login);
    		}
    
    		
    	}
    When an exception is raised, it prints it to the console, and suddenly the program no longer shows the >>> prompt from above. Any idea what I might be doing wrong?

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,368
    Blog Entries
    7
    Rep Power
    20

    Default Re: Losing control of the console

    The Console.readLine( ... ) method might throw an Exception that kills your loop; wrap it up in a try ... catch clause so you can know when something is wrong.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    pbj
    pbj is offline Member
    Join Date
    Jun 2014
    Posts
    15
    Rep Power
    0

    Default Re: Losing control of the console

    john,
    Is there any specific reason that you are using Console instead of Scanner? this might change your mind about Console.

Similar Threads

  1. Losing sleep over this
    By MW130 in forum New To Java
    Replies: 6
    Last Post: 02-02-2014, 06:44 PM
  2. Replies: 0
    Last Post: 11-22-2013, 05:19 AM
  3. Losing data from array - not sure why
    By neveser in forum New To Java
    Replies: 8
    Last Post: 12-02-2012, 07:19 AM
  4. JTabbedPane losing listener
    By mendescholl in forum AWT / Swing
    Replies: 1
    Last Post: 02-03-2012, 03:01 AM

Posting Permissions

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