Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

unblocking a read on System.in

Posted on 2001-07-25
4
Medium Priority
?
289 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 400 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

916 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