All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.datasketches.memory.Handle Maven / Gradle / Ivy

There is a newer version: 5.0.0
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package org.apache.datasketches.memory;

/**
 * A handle for read-only resource.
 *
 * 

The purpose of a Handle is to *

  • Provide a strong reference to an external resource.
  • *
  • Extend AutoCloseable, which provides a means to close the resource.
  • *
  • Provide other capabilites unique to a particular resource.
  • *
* *

Maintaining strong references to external resources is critical to avoid accidental * use-after-free scenarios, where the Garbage Collector will automatically close an external * resource if there are no remaining strong references to it. One very common mistake, is to allow * a newly created Handle to fall out-of-scope from the block where it was created, such as from a * try-with-resources statement. The Garbage Collector will eventually close the Handle referent * resource.

* *

Another use-after-free scenario is where a thread or agent, with access to the * Handle, prematurely closes a resource, when another part of the program is still using that * same resource. Avoiding this scenario requires careful planning and design.

* *

The design philosophy here is that whatever process created the external resource has the * responsibility to close() that resource when it is no longer needed. This responsibility * can be delegated, by passing the appropriate Handle to the delegatee. In principle, however, at * any one time there should be only one agent holding the Handle and responsible for closing the * resource.

* * @author Lee Rhodes * @author Roman Leventov */ public interface Handle extends AutoCloseable { /** * Gets a Memory * @return a Memory */ Memory get(); @Override void close(); }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy