JDK 10 RFR of 8147615: (fc) FileChannelImpl has no finalizer

Roger Riggs Roger.Riggs at Oracle.com
Sat Oct 7 15:57:41 UTC 2017


Hi Brian,

On 10/6/2017 7:06 PM, Brian Burkhalter wrote:
>
>> FileChannelImpl:
>>
>> - line 117:  A lambda could be used instead of the explicit Closer class.
>>
>>   () -> fdAccess.close(fd)
>>   // Note: fd is the argument (not the field) to prevent the lambda
>>   from capturing this.
>>
>> In the test:
>>  - Perhaps a bit more descriptive name than just "Cleaner" perhaps 
>> CleanerTest or...
>>
>>  - Add @modules java.management - so the test will not be run unless 
>> the runtime includes management
>
> I made the foregoing changes but now the test times out. I imagine 
> there is a problem with how I set up the lambda. Please see the diff 
> below.

I wonder if lambda is preferring this.fd instead of the argument fd.  (I 
imagine the JLS says that somewhere)
Try creating a new (final) local variable with the value of argument fd 
and using that in the lambda.

Otherwise, its fine.

Thanks, Roger


>
> Thanks,
>
> Brian
>
> --- a/src/java.base/share/classes/sun/nio/ch/FileChannelImpl.java
> +++ b/src/java.base/share/classes/sun/nio/ch/FileChannelImpl.java
> @@ -90,18 +90,6 @@
>      // Cleanable with an action which closes this channel's file 
> descriptor
>      private final Cleanable cleanable;
>
>
> -    private static class Closer implements Runnable {
> -        private final FileDescriptor fd;
> -
> -        Closer(FileDescriptor fd) {
> -            this.fd = fd;
> -        }
> -
> -        public void run() {
> -            fdAccess.close(fd);
> -        }
> -    }
> -
>      private FileChannelImpl(FileDescriptor fd, String path, boolean 
> readable,
>                              boolean writable, Object parent)
>      {
> @@ -113,8 +101,7 @@
>          this.nd = new FileDispatcherImpl();
>          // Register a cleaning action if and only if there is no parent
>          // as the parent will take care of closing the file descriptor.
> -        this.cleanable = parent != null ? null :
> - CleanerFactory.cleaner().register(this, new Closer(fd));
> +        this.cleanable = parent != null ? null : () -> 
> fdAccess.close(fd);
>      }
>
>
>      // Used by FileInputStream.getChannel(), FileOutputStream.getChannel
>
> rename from test/jdk/java/nio/channels/FileChannel/Cleaner.java
> rename to test/jdk/java/nio/channels/FileChannel/CleanerTest.java
> --- a/test/jdk/java/nio/channels/FileChannel/Cleaner.java
> +++ b/test/jdk/java/nio/channels/FileChannel/CleanerTest.java
> @@ -25,7 +25,8 @@
>   * @bug 8147615
>   * @summary Test whether an unreferenced FileChannel is actually cleaned
>   * @requires (os.family == "linux") | (os.family == "mac") | 
> (os.family == "solaris") | (os.family == "aix")
> - * @run main/othervm Cleaner
> + * @modules java.management
> + * @run main/othervm CleanerTest
>   */
>
>
>  import com.sun.management.UnixOperatingSystemMXBean;
> @@ -40,7 +41,7 @@
>  import java.nio.file.Paths;
>  import java.nio.file.StandardOpenOption;
>
>
> -public class Cleaner {
> +public class CleanerTest {
>      public static void main(String[] args) throws Throwable {
>          OperatingSystemMXBean mxBean =
> ManagementFactory.getOperatingSystemMXBean();
>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.openjdk.java.net/pipermail/nio-dev/attachments/20171007/6713e6b2/attachment.html>


More information about the nio-dev mailing list