Solved

unblocking a read on System.in

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Suggested Solutions

Title # Comments Views Activity
sql import cannot be resolved jsp 3 72
collection output issue 9 58
spring maven example issues 3 37
American Express @Work site and Java 4 63
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

685 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