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

    Default console freezes when working in different threads

    I have a console application. One thread allows a user to directly input into a console. Another thread listens on a tcp port, takes input, processes it, and then writes it to the console. The work in different threads, but in tcp thread, one I call a method outside that thread that writes to console, it often gets stuck. here is a mockup of situation:

    Java Code:
    import java.io.BufferedReader;
    import java.io.Console;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.PrintWriter;
    import java.net.ServerSocket;
    import java.net.Socket;
    
    public class ReproduceConsoleIssue {
    	private volatile boolean listening=true;
    	
    	private void test(){
    		Thread console = new Thread(new Runnable(){
    		    public void run() {
    		    	startConsole();
    		    }           
    		});
    		
    		Thread tcpServer = new Thread(new Runnable(){
    			public void run() {
    				startTcpServer();
    			}
    		});
    		
    		console.start();
    		tcpServer.start();
    	}
    	
    	private void startConsole(){
    		Console console = System.console();
    		if (console == null) {
    		    System.exit(1);
    		}
    		
    		while(listening){
    			String input = console.readLine("<console>: ");
    			console.writer().println(input);
    		}
    	}
    	
    	private void startTcpServer(){
    		try (
    				ServerSocket tcpSocket = new ServerSocket(3001);
    		) {			
    			while(listening){
    				Socket clientSocket = tcpSocket.accept();
    				String clientMsg;
    				PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
    				BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
    				
    				 if ( (clientMsg = in.readLine()).length() > 0 ) {
    					 Unit unit = new Unit();
    					 String resp = unit.send();
    					 System.out.println("msg " + clientMsg + " response " + resp);
    				 }
    				 out.println(clientMsg);
    			}
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    	}
    	
    	public static void main(String[] args) {
    		ReproduceConsoleIssue test = new ReproduceConsoleIssue();
    		test.test();
    	}
    
    }
    
    public class Unit {
    	public Unit(){
    		
    	}
    	
    	public String send(){
    		for(int i=0; i<3;i++){
    			System.out.println("Attempt " + i+1);
    		}
    		return "ack 1";
    	}
    }
    It often freezes on "Attempt 1". Before I used System.out there, I also tried console.writer() there but both freeze at that point often. Anyone ever experience a situation where console or System.out.writeln freeze when working across threads and why it is occurring? It almost feels like one thread has locked the console so the others can't write to it.
    Last edited by johnmerlino; 07-22-2014 at 08:10 AM.

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

    Default Re: console freezes when working in different threads

    I don't know if it matters (much) but one thread is using the Console for its IO, while another thread uses System.out for its IO. The Console uses synchronized methods for its IO ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    johnmerlino is offline Member
    Join Date
    May 2014
    Posts
    56
    Rep Power
    0

    Default Re: console freezes when working in different threads

    I was initially using Console for both but that was getting stuck too. And I know it was because I added some debugging around it and it would never get passed that point. Maybe it's not an issue with the Console itself. I am synchronizing a block which surrounds the write to the console. Could that be it?

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

    Default Re: console freezes when working in different threads

    Try to use the simple System.in and System.out objects in all cases and see what happens.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    johnmerlino is offline Member
    Join Date
    May 2014
    Posts
    56
    Rep Power
    0

    Default Re: console freezes when working in different threads

    I'll try that out. thanks for responses

Similar Threads

  1. Program freezes please help
    By phyzix5761 in forum New To Java
    Replies: 14
    Last Post: 11-16-2012, 04:53 AM
  2. Replies: 3
    Last Post: 05-09-2012, 09:35 AM
  3. Replies: 4
    Last Post: 11-03-2011, 09:19 PM
  4. Server Socket GUI Freezes
    By mrhid6 in forum Threads and Synchronization
    Replies: 12
    Last Post: 09-30-2011, 04:39 PM
  5. Working with threads.
    By bpx95 in forum New To Java
    Replies: 3
    Last Post: 04-06-2011, 06:24 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
  •