Solved

unblocking a read on System.in

Posted on 2001-07-25
4
281 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
  • 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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This video teaches viewers about errors in exception handling.

831 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