Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.
Hi all, please see this major site announcement: https://www.boards.ie/discussion/2058427594/boards-ie-2026

Java non-blocking I/O operations....?

  • 15-11-2007 04:29PM
    #1
    Registered Users, Registered Users 2 Posts: 2,835 ✭✭✭


    Folks,

    I'm trying to close a thread in my program, but it wont let me as there is an I/O call in the thread that is blocking and waiting for input

    this is the run method inside my thread class
    
    class myThreadName extends Thread
    {
    public void run() 
    	{
    			
    			Scanner in = new Scanner(System.in);
    			int input = in.nextInt();
    			if (input >= myObject.nextPrice())
    			{
    				// print to the console
    				// call some method
    			}
    			
    			
    	}
    }
    

    I'm instantiaing a new thread in the main program and using mythreadname.start() to get it going. mythreadname.stop() wont stop it and neither will suspend(), interrupt() etc...

    Does Java have any non-blocking I/O operations in its API? even some sort of I/O timeout might work if its there but i cant find anything helpful in google

    Cheers Lads... any ideas appreciated


Comments

  • Registered Users, Registered Users 2, Paid Member Posts: 2,033 ✭✭✭lynchie


    Most of the java nio packages allow you to interrupt blocked IO as does the Socket code.. AFAIK, reading from the console cannot be interrupted. Your code will block indefinitely until somebody hits enter on the keyboard.


  • Registered Users, Registered Users 2 Posts: 2,835 ✭✭✭StickyMcGinty


    cheers lynchie, i managed to get it in the end by setting the thread as a Daemon thread, useful for anyone with the same problem


Advertisement
Advertisement