Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

input stream question

Status
Not open for further replies.

Cagliostro

Programmer
Sep 13, 2000
4,226
GB
Hi, I have an input stream like:
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

Further operation is
str = in.readLine();
it stops the thread until can read something from the stream. How can I know if in the stream is something to read without stopping application or thread? Ion Filipski
1c.bmp


filipski@excite.com
 
Generally you cant.

Because the socket does block until data is available it is pretty normal to run it in a separate thread, so it can sit idle for as long as it wants without effecting the main thread, and then when data arrives it can pass it along to the main thread. -------------------------------------------
There are no onions, only magic
-------------------------------------------
 
see

void run()
{
while(toRun)
{
in.readLine();
//the problem is what toRun is set to false while
//in.readLine is not executed this thread will never stop
//if I do not use threadx.stop
}
}

I jhust have found what to do

void run()
{
while(toRun)
{
if(in.ready())in.readLine();
}
}

Ion Filipski
1c.bmp


filipski@excite.com
 
>> I jhust have found what to do

no u have not. u created a processor intensive application also known as a processor hog.

just as jfryer stated u should do the read of the stream in a worker thread. when u want to unblock that stream.read call u just close the socket.

-There are only 10 types of people in the world, those who understand binary and those who don't-

-pete
 
I have not asked if I have to do so or not. I asked how to do it. Ion Filipski
1c.bmp


filipski@excite.com
 
>> I have not asked if I have to do so or not. I asked how
>> to do it.

Sorry i don't understand that. do u have the answer to this post yet? -There are only 10 types of people in the world, those who understand binary and those who don't-

-pete
 
As you can see, I have posted it already in my own responce to my question :( Ion Filipski
1c.bmp


filipski@excite.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top