Killing thread that is doing blocking WRITE.

Hello,

I got a problem with threads: I have to write something (currently using FileWriter) to a device (file) which is not always available for writing - the write()/flush() function is being blocked in such case. I also have to implement timeout - if write()/flush() aren't able to accomplish writing in the given period of time, the write should be aborted.

My solution is to create new thread responsible for writing and wait with join(timeout) or notify()/wait(timeout) until it finishes. But what should I do when the thread didn't succeed (write()/flush() is still blocked)?

I have to finish the thread somehow. But I can't use stop() neither interrupt() (deprecated/not working while blocking IO is in progress).

The method with closing stream doesn't work, because close() calls flush() - which blocks the parent thread either. I guess it only works for reading, not for writing (well, maybe if the buffer was empty - but that's not my case). Maybe there is a way to close() an OutputStream without flushing its contents?

Any ideas?

Thanks,

Robert

[1124 byte] By [rthomanek] at [2007-9-26 1:29:45]
# 1
You may try to destroy the thread. Tell me if it works.
igarn at 2007-6-29 1:26:48 > top of Java-index,Archived Forums,Java Programming...
# 2
I think I didn't understand your idea... Do you mean destroying the thread (if so, how?) or destroying the process (system, not Java VM)?Robert
rthomanek at 2007-6-29 1:26:48 > top of Java-index,Archived Forums,Java Programming...
# 3
Hi,class Thread provide the destroy() method but it actually do nothing... Also forgot it!Regards,Martin
edosoft at 2007-6-29 1:26:48 > top of Java-index,Archived Forums,Java Programming...
# 4

Hi Robert,

I think your problem is the driver of device you are writing to. Why it doesn't throw an IOException if the device is not available? May be you should try to access it in other way first - to check if it's available - and not attempt to write if it's not.

Hope it helps,

Martin

edosoft at 2007-6-29 1:26:48 > top of Java-index,Archived Forums,Java Programming...
# 5
Well, the "driver" is Windows LPT1 device... :-)Unfortunately, I can't get it's status - I mean check whether the printer is available or not. If you have any ideas on doing it, I would appreciate them (Same problem applies to serial printer on COM1).Robert
rthomanek at 2007-6-29 1:26:48 > top of Java-index,Archived Forums,Java Programming...
# 6

If your output device is the printer try to use the printing API. The Java 1.2 Package java.awt.print provides all services you need. For example the print() method of PrinterJob throws the PrinterIOException if the communication with the printer fails (hopefully ;).

Regards,

Martin

edosoft at 2007-6-29 1:26:48 > top of Java-index,Archived Forums,Java Programming...
# 7

The problem is that I HAVE to use LPT1/COM1 - because it is a special kind of line printer. I'm currently thinking of javax.comm but I don't consider it to be a real solution (portability trouble).

Generally, I have to print text on a printer using file device. No problem on UNIX but Windows makes a hard time...

Robert

rthomanek at 2007-6-29 1:26:48 > top of Java-index,Archived Forums,Java Programming...
# 8
And by the way, there seems to be an updated version of commapi (javax.comm) - for Solaris, the lastest version is 2.0.2 (Windows - 2.0). Does anybody know what has been fixed/added? Is there a changelog somewhere?Robert
rthomanek at 2007-6-29 1:26:48 > top of Java-index,Archived Forums,Java Programming...