Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > new FileWriter hangs

Reply
Thread Tools

new FileWriter hangs

 
 
mattias
Guest
Posts: n/a
 
      07-31-2005
Hello. I have a java program where I repeatedly write to a log file,
and at random times (typically after 100.000 writes or so) the program
just hangs. I've found that this happens while executing a 'new
FileWriter' command. I only have one thread writing to the file,
there's no exceptions thrown or anything, the CPU usage just goes to
zero and then nothing happens at all. Meanwhile, the log file remains
globally locked for writing, i.e. I can't delete it in Explorer without
first killing my java program. I experience this bug on a dual xeon
processor machine, running Windows Server 2003 and java version
1.5.0_04.

Here's the code, it hangs while executing the very first line of code:

FileWriter fileWriter = new FileWriter("log.txt", true);
BufferedWriter buffWriter = new BufferedWriter(fileWriter);
buffWriter.write(message);
buffWriter.newLine();
buffWriter.close();

If anyone has any ideas why this happens, please let me know. I guess
in this case I could leave the file open at all times to avoid this,
but I would be more interested to know the cause of it.

Mattias

 
Reply With Quote
 
 
 
 
HGA03630@nifty.ne.jp
Guest
Posts: n/a
 
      07-31-2005

mattias wrote:
> Hello. I have a java program where I repeatedly write to a log file,
> and at random times (typically after 100.000 writes or so) the program
> just hangs. I've found that this happens while executing a 'new
> FileWriter' command. I only have one thread writing to the file,
> there's no exceptions thrown or anything, the CPU usage just goes to
> zero and then nothing happens at all. Meanwhile, the log file remains
> globally locked for writing, i.e. I can't delete it in Explorer without
> first killing my java program. I experience this bug on a dual xeon
> processor machine, running Windows Server 2003 and java version
> 1.5.0_04.
>
> Here's the code, it hangs while executing the very first line of code:
>
> FileWriter fileWriter = new FileWriter("log.txt", true);
> BufferedWriter buffWriter = new BufferedWriter(fileWriter);
> buffWriter.write(message);
> buffWriter.newLine();
> buffWriter.close();
>
> If anyone has any ideas why this happens, please let me know. I guess
> in this case I could leave the file open at all times to avoid this,
> but I would be more interested to know the cause of it.
>
> Mattias


 
Reply With Quote
 
 
 
 
jan V
Guest
Posts: n/a
 
      07-31-2005
> If anyone has any ideas why this happens, please let me know. I guess
> in this case I could leave the file open at all times to avoid this,
> but I would be more interested to know the cause of it.


Sounds like a deadlock doesn't it? Have you pressed the magic keys to
provide a dump of the JVM's state (which includes deadlock info) ?

... from the Java docs:

"A deadlock detection utility has been added to the Java HotSpot VM. The
utility is invoked by a Ctrl+\ on the command line while an application is
running. The utility detects Java-platform-level deadlocks, including
locking done from the Java Native Interface (JNI), the Java Virtual Machine
Profiler Interface (JVMPI), and Java Virtual Machine Debug Interface
(JVMDI).

When invoked, the utility displays a thread dump to standard out and
indicates any Java-platform-level deadlocks it detects. Refer to this sample
output. If the application is deadlocked because two or more threads are
involved in a cylce to acquire monitors, then the list of such threads and
monitors involved in the deadlocks are displayed. Note, however, that this
will not find deadlocks involving threads waiting on monitors on which no
signal will be forthcoming. "


 
Reply With Quote
 
Thomas Hawtin
Guest
Posts: n/a
 
      07-31-2005
mattias wrote:
>
> Here's the code, it hangs while executing the very first line of code:
>
> FileWriter fileWriter = new FileWriter("log.txt", true);
> BufferedWriter buffWriter = new BufferedWriter(fileWriter);
> buffWriter.write(message);
> buffWriter.newLine();
> buffWriter.close();


As jan says, looks like a dead lock. However the code isn't safe as you
don't guarantee to close the FileWriter. It should look something like:

FileWriter fileWriter = new FileWriter("log.txt", true);
try {
BufferedWriter buffWriter = new BufferedWriter(fileWriter);
buffWriter.write(message);
buffWriter.newLine();
buffWriter.close();
} finally {
fileWriter.close();
}

In any case opening and closing a file that fast doesn't seem like a
good idea. It's heavy on operating system resources and also on
finalisers. If multiple processes do need to access the single file,
then you are quite likely to have lots of exceptions as they collide.

If it is true that multiple processes need to access the file, perhaps a
better route is to use RandomAccessFile. From that you can get the
java.nio.channels.FileChannel which allows locking and unlocking
exclusive access to the file without opening and closing. I must admit
that I have not tried that myself.

Tom Hawtin
--
Unemployed English Java programmer
http://jroller.com/page/tackline/
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      08-01-2005
On Sun, 31 Jul 2005 16:19:19 +0100, Thomas Hawtin
<> wrote or quoted :

>If it is true that multiple processes need to access the file, perhaps a
>better route is to use RandomAccessFile.


He is just tacking on the end.

Just write a synchronised method to write one log entry, and leave the
file open, or use the autoflush if you are concerned about losing the
tail on a crash.

--
Bush crime family lost/embezzled $3 trillion from Pentagon.
Complicit Bush-friendly media keeps mum. Rumsfeld confesses on video.
http://www.infowars.com/articles/us/...s_rumsfeld.htm

Canadian Mind Products, Roedy Green.
See http://mindprod.com/iraq.html photos of Bush's war crimes
 
Reply With Quote
 
Thomas Hawtin
Guest
Posts: n/a
 
      08-01-2005
Roedy Green wrote:
> On Sun, 31 Jul 2005 16:19:19 +0100, Thomas Hawtin
> <> wrote or quoted :
>
>
>>If it is true that multiple processes need to access the file, perhaps a
>>better route is to use RandomAccessFile.

>
>
> He is just tacking on the end.
>
> Just write a synchronised method to write one log entry, and leave the
> file open, or use the autoflush if you are concerned about losing the
> tail on a crash.


The reason I suggested RandomAccessFile was for the case where there are
more than one process appending to the same file. If you try that with
FileWriter/FileOutputStream kept open, you will rapidly run out of luck.
Perhaps just viewing the file causes problems.

OTOH, if it is just from the same process (and accessible) then,
absolutely, a long lived FileWriter/OutputStreamWriter(FileOutputStream)
is the way to go.

Tom Hawtin
--
Unemployed English Java programmer
http://jroller.com/page/tackline/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Diff. between FileWriter("f.txt") and OutputStreamWriter(new FileOutputStream("f.txt")) ? Jochen Brenzlinger Java 7 09-15-2011 01:23 AM
Clear hangs up - & hangs up - & hangs up Sue Bilstein NZ Computing 26 03-07-2004 01:33 AM
Cant find java.io.FileWriter? bigbinc Java 3 12-18-2003 05:58 PM
Problem with FileWriter class Nimmi Srivastav Java 7 08-19-2003 05:47 AM
FileWriter Problem Rahul Sharma Java 1 07-23-2003 06:06 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57