Solved

unblocking a read on System.in

Posted on 2001-07-25
4
285 Views
Last Modified: 2012-05-04
how can i unblock a read on System.in and other InputStreams, from a different thread or whatever?

thanks
0
Comment
Question by:glow060197
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 3

Accepted Solution

by:
Sendoh earned 100 total points
ID: 6320500
hi !
u can just create thread 1 the read from System.in...
then create another thread 2 to close the system.in...
after that from thread1 it'll receive an exception...
this might work for ur case anyway...:)

hope it helps.
regards
Sendoh
0
 
LVL 3

Expert Comment

by:stimpyjcat
ID: 6321926
As Sendoh suggests, closing a stream will cause any pending reads to return.

Alternatively, you can change the read loops to only invoke read when there are bytes pending to be read, i.e. available returns non-zero.

boolean done = false;
while (!done) {
     if (in.available()) in.read()
     // sleep or wait
}


Then another thread can simply set done = true in order to stop the reading thread.
0
 
LVL 1

Expert Comment

by:ylyip
ID: 6325581
Just want to point out that there may be a synchronization problem. When 2 threads are working on the same stream object, one read may lock the object and disallow the other to close it.
0
 
LVL 3

Expert Comment

by:stimpyjcat
ID: 6326410
Neither the "read" nor the "close" methods on a stream are synchronized.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question