java.lang.AutoCloseable类的使用及代码示例

x33g5p2x  于2022-01-15 转载在 其他  
字(5.0k)|赞(0)|评价(0)|浏览(272)

本文整理了Java中java.lang.AutoCloseable类的一些代码示例,展示了AutoCloseable类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。AutoCloseable类的具体详情如下:
包路径:java.lang.AutoCloseable
类名称:AutoCloseable

AutoCloseable介绍

[英]Defines an interface for classes that can (or need to) be closed once they are not used any longer. Calling the close method releases resources that the object holds.

A common pattern for using an AutoCloseable resource:

Closable foo = new Foo();finally  
foo.close(); 
} 
}

[中]为不再使用后可以(或需要)关闭的类定义接口。调用close方法释放对象所持有的资源。
使用自动关闭资源的常见模式:

Closable foo = new Foo();finally  
foo.close(); 
} 
}

代码示例

代码示例来源:origin: apache/flink

/**
 * Closes the given AutoCloseable.
 *
 * <p><b>Important:</b> This method is expected to never throw an exception.
 */
public static void closeQuietly(AutoCloseable closeable) {
  try {
    if (closeable != null) {
      closeable.close();
    }
  } catch (Throwable ignored) {}
}

代码示例来源:origin: dropwizard/dropwizard

/**
   * Calls {@link AutoCloseable#close()} on the closable provided in
   * {@link AutoCloseableManager#AutoCloseableManager(AutoCloseable)}.
   *
   * @throws Exception propagates {@link AutoCloseable#close()} exception
   */
  @Override
  public void stop() throws Exception {
    this.autoCloseable.close();
  }
}

代码示例来源:origin: requery/requery

private static void closeSuppressed(AutoCloseable closeable) {
  if (closeable != null) {
    try {
      closeable.close();
    } catch (Exception ignored) {
    }
  }
}

代码示例来源:origin: deathmarine/Luyten

public static void tryClose(final AutoCloseable c) {
  if (c == null) {
    return;
  }
  try {
    c.close();
  } catch (Throwable ignored) {
  }
}

代码示例来源:origin: looly/hutool

/**
 * 关闭<br>
 * 关闭失败不会抛出异常
 * 
 * @param closeable 被关闭的对象
 */
public static void close(AutoCloseable closeable) {
  if (null != closeable) {
    try {
      closeable.close();
    } catch (Exception e) {
      // 静默关闭
    }
  }
}

代码示例来源:origin: thinkaurelius/titan

public static void closeQuietly(AutoCloseable c) {

    try {
      if (c != null)
        c.close();
    } catch (Exception e) {
      logger.warn("Failed closing " + c, e);
    }
  }
}

代码示例来源:origin: prestodb/presto

private static void closeUnchecked(AutoCloseable closeable)
{
  try {
    closeable.close();
  }
  catch (Exception e) {
    throwIfUnchecked(e);
    throw new RuntimeException(e);
  }
}

代码示例来源:origin: apache/kafka

/**
 * Closes {@code closeable} and if an exception is thrown, it is logged at the WARN level.
 */
public static void closeQuietly(AutoCloseable closeable, String name) {
  if (closeable != null) {
    try {
      closeable.close();
    } catch (Throwable t) {
      log.warn("Failed to close {} with type {}", name, closeable.getClass().getName(), t);
    }
  }
}

代码示例来源:origin: SonarSource/sonarqube

/**
  * Closes the object and throws an {@link java.lang.IllegalStateException} on error.
  * @since 5.1
  */
 public void close(AutoCloseable closeable) {
  try {
   closeable.close();
  } catch (Exception e) {
   throw new IllegalStateException("Fail to close " + closeable, e);
  }
 }
}

代码示例来源:origin: Alluxio/alluxio

private void closeQuietly(AutoCloseable c) {
 if (c != null) {
  try {
   c.close();
  } catch (Throwable t) {
   if (mThrown != null) {
    mThrown.addSuppressed(t);
   } else {
    mThrown = t;
   }
  }
 }
}

代码示例来源:origin: thinkaurelius/titan

public synchronized void release() throws Exception {
    Preconditions.checkState(null != current);
    Preconditions.checkState(0 < refCount);
    refCount--;
    if (0 == refCount) {
      current.close();
      current = null;
    }
  }
}

代码示例来源:origin: Tencent/tinker

/**
   * Closes the given {@code obj}. Suppresses any exceptions.
   */
  @SuppressWarnings("NewApi")
  public static void closeQuietly(Object obj) {
    if (obj == null) return;
    try {
      if (obj instanceof Closeable) {
        ((Closeable) obj).close();
      } else if (obj instanceof AutoCloseable) {
        ((AutoCloseable) obj).close();
      } else if (obj instanceof ZipFile) {
        ((ZipFile) obj).close();
      }
    } catch (Throwable ignored) {
      // ignored.
    }
  }
}

代码示例来源:origin: JanusGraph/janusgraph

public static void closeQuietly(AutoCloseable c) {

    try {
      if (c != null)
        c.close();
    } catch (Exception e) {
      logger.warn("Failed closing " + c, e);
    }
  }
}

代码示例来源:origin: apache/kylin

/**
 * To close a <C>AutoCloseable</C> implementation, such as <C>java.sql.Connection</C>, <C>java.sql.Statement</C>, <C><java.sql.ResultSet/C>.
 * @param closeable Such as <C>java.sql.Connection</C>, <C>java.sql.Statement</C>, <C><java.sql.ResultSet/C>.
 */
protected void close(AutoCloseable closeable) {
  try {
    closeable.close();
  } catch (Exception e) {
    throw new IllegalStateException(e);
  }
}

代码示例来源:origin: JpressProjects/jpress

private static final void close(AutoCloseable... sts) {
  for (AutoCloseable st : sts)
    if (st != null) {
      try {
        st.close();
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
}

代码示例来源:origin: apache/kylin

public static void closeQuietly(AutoCloseable obj) {
  if (obj != null) {
    try {
      obj.close();
    } catch (Exception e) {
      logger.warn("Error closing " + obj, e);
    }
  }
}

代码示例来源:origin: brianway/webporter

private void destroyEach(Object object) {
  if (object instanceof AutoCloseable) {
    try {
      ((AutoCloseable) object).close();
    } catch (Exception e) {
      logger.warn("destroyEach: {}", e);
    }
  }
}

代码示例来源:origin: prestodb/presto

static void closeQuietly(AutoCloseable closeable)
  {
    try {
      closeable.close();
    }
    catch (Exception ignored) {
    }
  }
}

代码示例来源:origin: apache/geode

private static void closeDataSource(Object dataSource) {
 if (dataSource instanceof AutoCloseable) {
  try {
   ((AutoCloseable) dataSource).close();
  } catch (Exception e) {
   if (logger.isDebugEnabled()) {
    logger.debug("Exception closing DataSource", e);
   }
  }
 }
}

代码示例来源:origin: neo4j/neo4j

@Override
  public void shutdown() throws Exception
  {
    closeable.close();
  }
}

相关文章