[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
RE: Patch: File.deleteOnExit()
From: |
Jeroen Frijters |
Subject: |
RE: Patch: File.deleteOnExit() |
Date: |
Tue, 18 May 2004 09:46:31 +0200 |
Hi,
Thanks for the explanation! In that case I would suggest (and this hopefully
also addresses Mark's points):
+package java.io;
+
+final class DeleteFileHelper extends Thread
+{
+ private static ArrayList filesToDelete;
+
+ static synchronized void add(File file)
+ {
+ if (filesToDelete == null)
+ {
+ filesToDelete = new ArrayList();
+ AccessController.doPrivileged(new PrivilegedAction()
+ {
+ public Object run()
+ {
+ try
+ {
+ Runtime.getRuntime().addShutdownHook(new DeleteFileHelper());
+ }
+ catch (IllegalStateException x)
+ {
+ // shutdown is already in progress, so we can't register ours
+ }
+ return null;
+ }
+ });
+ }
+ filesToDelete.add(file);
+ }
+
+ private static synchronized deleteFiles()
+ {
+ Iterator it = filesToDelete.iterator();
+
+ while (it.hasNext())
+ {
+ try
+ {
+ File file = (File) it.next();
+ file.delete();
+ }
+ catch (Exception e)
+ {
+ // Do nothing here.
+ }
+ }
+ }
+
+ private DeleteFileHelper()
+ {
+ }
+
+ public void run()
+ {
+ deleteFiles();
+ }
+}
Regards,
Jeroen
> -----Original Message-----
> From: Ingo Prötel [mailto:address@hidden
> Sent: Tuesday, May 18, 2004 08:55
> To: Jeroen Frijters
> Cc: Michael Koch; address@hidden
> Subject: Re: Patch: File.deleteOnExit()
>
> Hi,
>
> static initializers in our case (a real-time vm) run at
> startup. Otherwise we would need to add all
> static initializers to the worst case execution time for
> everything. For real-time purposes we need
> to have all classes loaded and initialized before the
> application starts.
> If we have the creation in a method that will never gets
> executed we can remove the code.
> So in general we prefere lazy initialization over static
> initializers. If we need good response we
> can force an early initialization but we do not need to
> commit resources that might never be used.
>
> Cheers,
> ingo
>
> Jeroen Frijters wrote:
> > Hi,
> >
> > The static initializer only runs when add() is called for
> the first time.
> >
> > Regards,
> > Jeroen
> >
> >
> >>-----Original Message-----
> >>From: Ingo Prötel [mailto:address@hidden
> >>Sent: Monday, May 17, 2004 19:27
> >>To: Jeroen Frijters
> >>Cc: Michael Koch; address@hidden
> >>Subject: Re: Patch: File.deleteOnExit()
> >>
> >>Hi,
> >>
> >>I would vote for Michaels solution. Having a static
> >>initializer that creates a Thread which might
> >>never be used seems wasteful.
> >>
> >>Cheers,
> >>ingo
> >>
> >>Jeroen Frijters wrote:
> >>
> >>>Michael Koch wrote:
> >>>
> >>>
> >>>>Am Montag, 17. Mai 2004 12:40 schrieb Jeroen Frijters:
> >>>>
> >>>>
> >>>>>Michael Koch wrote:
> >>>>>
> >>>>>
> >>>>>>The attached patch written by Guilhem Lavaux implements
> >>>>>>File.deleteOnExit() in an easy way.
> >>>>>>
> >>>>>>Ok to commit ?
> >>>>>
> >>>>>It doesn't make me happy to preallocate all these objects (and in
> >>>>>general, the less static initializers the better). Shouldn't we
> >>>>>just move DeleteFileHelper to gnu.java.io (or whatever)
> and make it
> >>>>>a proper singleton? That way the singleton will be
> allocated lazily
> >>>>>upon first use of File.deleteOnExit().
> >>>>
> >>>>Here is a revised patch for review:
> >>>
> >>>
> >>>Thanks! I think it would be slightly better to do:
> >>>
> >>>+public final class DeleteFileHelper extends Thread
> >>>+{
> >>>+ private static LinkedList filesToDelete = new LinkedList();
> >>>+
> >>>+ static
> >>>+ {
> >>>+ Runtime.getRuntime().addShutdownHook(new DeleteFileHelper());
> >>>+ }
> >>>+
> >>>+ public static void add(File file)
> >>>+ {
> >>>+ filesToDelete.add(file);
> >>>+ }
> >>>+
> >>>+ private DeleteFileHelper()
> >>>+ {
> >>>+ }
> >>>
> >>>Since we already had a static initializer (to initialize the static
> >>>filesToDelete field), it's better to leverage it to do the call to
> >>>addShutdownHook as well. I also made the constructor private.
> >>>
> >>>Regards,
> >>>Jeroen
> >>>
> >>>
> >>>_______________________________________________
> >>>Commit-classpath mailing list
> >>>address@hidden
> >>>http://mail.gnu.org/mailman/listinfo/commit-classpath
> >>>
> >>
> >>--
> >>Ingo Prötel
> address@hidden
> >>aicas GmbH
> http://www.aicas.com
> >>Haid-und-Neu-Str. 18 phone +49 721
> 663 968-32
> >>76131 Karlsruhe fax +49 721
> 663 968-93
> >>Germany
> >
> >
> >
>
> --
> Ingo Prötel address@hidden
> aicas GmbH http://www.aicas.com
> Haid-und-Neu-Str. 18 phone +49 721 663 968-32
> 76131 Karlsruhe fax +49 721 663 968-93
> Germany
>
- Re: Patch: File.deleteOnExit(), (continued)
- RE: Patch: File.deleteOnExit(),
Jeroen Frijters <=