Path: csiph.com!usenet.pasdenom.info!news.albasani.net!.POSTED!not-for-mail From: Jan Burse Newsgroups: comp.lang.java.programmer Subject: Re: Making System.in interruptible, how? Date: Fri, 09 Mar 2012 20:01:00 +0100 Organization: albasani.net Lines: 44 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-15; format=flowed Content-Transfer-Encoding: 7bit X-Trace: news.albasani.net SDGr1Pw7ga6M67jk+rpcwH6OxbEjXNQ8AS5svvFIxfexAJ1w4M4Cen/FzA7TSrk2fSli0CgzEvwQCEs57tNTn+5yyQaojHhEzi3dMcUjCGyoUAJ/o5YOkoO+CB9Qa2TU NNTP-Posting-Date: Fri, 9 Mar 2012 19:01:02 +0000 (UTC) Injection-Info: news.albasani.net; logging-data="8YlZoMCQKm6z/Dl7T4lU606MmSFzlegnez6F+mclkOrGzcOI98GIT+5dVhiNn2WVwgtTHc4NMa1Vxs/hwJv9agj3RtMfF58WeVWYwE1yQ4rNC0QaVdjT9MRAlxQsXQRS"; mail-complaints-to="abuse@albasani.net" User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:10.0.2) Gecko/20120216 Firefox/10.0.2 SeaMonkey/2.7.2 In-Reply-To: Cancel-Lock: sha1:2e985yjWi8HFtXhPPxelT5+ma4o= Xref: csiph.com comp.lang.java.programmer:12812 Jan Burse schrieb: > Dear All, > > Found the following nice article, highlighting how > it would be possible to make System.in interrupible: > > http://www.javaspecialists.eu/archive/Issue153.html > > Is this the preferred way to do? i.e. to use some > polling, or are there other ways around? > > Best Regards I have finally adopted the ready() solution. I guess that if the input stream is redirected to a file, the ready() will mostly return true, so that performance is no issue. An issue I had was that I wanted also the possibility that the input stream is hitchiked by an INT handler. The following worked for me: try { for (;;) { notPauseScreen(); if (!super.ready()) { Thread.sleep(SLEEP); } else { return super.read(cbuf, offset, length); } } } catch (InterruptedException x) { throw new InterruptedIOException(); } The INT handler will pause the screen. So when the screen is paused neither ready nor read are called. And the INT handler can call read on System.in, so as to query the end user for some action. When the INT handler has determined the action, it can resume the screen. And the normal application will continue getting input. Bye