Solved

unblocking a read on System.in

Posted on 2001-07-25
4
279 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Neither the "read" nor the "close" methods on a stream are synchronized.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
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…
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 …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

771 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now