`

Java cleanup code

阅读更多

Java shutdown hook guarantee that clean-up code is always run, regardless of how the user terminates the application.

Here is an example from Hadoop:

java 代码
  1. /**  
  2.  * A class to track the list of DFS clients, so that they can be closed  
  3.  * on exit.  
  4.  * @author Owen O'Malley  
  5.  */  
  6. private static class ClientFinalizer extends Thread {   
  7.   private List clients = new ArrayList();   
  8.   
  9.   public synchronized void addClient(DFSClient client) {   
  10.     clients.add(client);   
  11.   }   
  12.   
  13.   public synchronized void run() {   
  14.     Iterator itr = clients.iterator();   
  15.     while (itr.hasNext()) {   
  16.       DFSClient client = (DFSClient) itr.next();   
  17.       if (client.running) {   
  18.         try {   
  19.           client.close();   
  20.         } catch (IOException ie) {   
  21.           System.err.println("Error closing client");   
  22.           ie.printStackTrace();   
  23.         }   
  24.       }   
  25.     }   
  26.   }   
  27. }   
  28.   
  29. // add a cleanup thread   
  30. private static ClientFinalizer clientFinalizer = new ClientFinalizer();   
  31. static {   
  32.   Runtime.getRuntime().addShutdownHook(clientFinalizer);   
  33. }  

Elegant and useful!!!

 

 

分享到:
评论

相关推荐

    Clean Code

    But if code isn’t clean, it can bring a development organization to its knees. Every year, countless hours and significant resources are lost because of poorly written code. But it doesn’t have to ...

    VSCodeJavaInstaller-online-win-0.2.2.exe

    Visual Studio Code works with all major Java versions from various vendors up to 13. To help you get started quickly, we created a special Installer of Visual Studio Code for Java developers. The ...

    Programming.Google.App.Engine.with.Java

    Because App Engine supports common Java API standards, your code stays clean and portable. Get a hands-on introduction to App Engine's tools and features, using an example application Simulate App ...

    Test-Driven Java Development - Second Edition.pdf

    This book will teach the concepts of test driven development in Java so you can build clean, maintainable and robust code About This Book Explore the most popular TDD tools and frameworks and become...

    Java By Comparison Become a Java Craftsman in 70 Examples

    Write code that's clean, concise, and to the point: code that others will read with pleasure and reuse. Comparing your code to that of expert programmers is a great way to improve your coding skills. ...

    eclipse的代码格式化配置文件

    压缩包中包含了checkstyle-profile.xml,cleanup-profile.xml,codetemplates.xml,formatter-profile.xml四个代码格式化的配置文件,只需要在eclipse->window->preferences->java->code style中import对应的xml即可...

    java版飞机大战源码-reservation:PAC预约工具

    清理:cleanup.xml 命名约定 Eclipse 项目名称约定 名称 评论 啊啊啊 pom 项目作为所有项目的父项目 aaa-common 通用代码:实用程序、常量、异常、... aaa-*-客户端 实体、服务接口 aaa-*-服务 EJB,服务实现 aaa-...

    Thinking in Java 4th Edition

    You must perform cleanup ......... 121 The termination condition ......... 121 How a garbage collector works .. 122 Member initialization ......... 125 Specifying initialization ............. 126 ...

    Apache.Maven.Cookbook.1785286129

    Furthermore, the book walks you through setting up and using Maven with popular Java Integrated Development Environments (IDEs) so you can exploit Maven features to build standard Java applications....

    HTTP_Debugger_Pro_V9.10_Crack.zip

    Debug HTTP API calls to a back-end and between back-ends, Easy to use, clean UI, and short ramp-up time. Not a proxy, no network issues! Doesn't change the browser's configuration for the proxy. Can ...

    UE(官方下载)

    Create a custom user tool to compile Java code, using the command line, from within UltraEdit Configure UltraEdit with javascript lint How to check your JavaScript source code for common mistakes ...

    Google C++ Style Guide(Google C++编程规范)高清PDF

    Exceptions to the Rules Existing Non-conformant Code Windows Code Important Note Displaying Hidden Details in this Guide link ▶This style guide contains many details that are initially hidden from ...

    code-challenge

    怎么跑要求Java 11 玛文码头工人脚步您只需要运行以下命令: mvn clean package // to generate the jardocker build .docker-compose up -d为了方便起见,还有一个名为build_and_start.sh的文件,该文件完全执行...

    Fast Data Processing with Spark

    Fast Data Processing with Spark covers everything from setting up your Spark cluster in a variety of situations (stand-alone, EC2, and so on), to how to use the interactive shell to write distributed ...

    Android Apps for Absolute Beginners: Covering Android 7

    It provides clean, straightforward examples that make learning easy, allowing you to pick up the concepts without fuss. It offers clear code descriptions and layout so that you can get your apps ...

    selenium-dotnet-2.45.0.zip

    Code cleanup and refactoring will take place under a separate commit. Note that there is still no support in the .NET bindings for the Presto-based Opera without using the remote server, nor is ...

    MovieGuide:电影发现应用程序,展示MVP,RxJava,Dagger 2和Clean Architecture

    :thumbs_up: 目前不接受与应用程序的体系结构和功能有关的外部PR。 :stop_sign: 列出热门/收视率最高的电影,显示预告片和评论的Android应用。 该应用程序展示了MVP模式,RxJava,Dagger 2和Bob Martin叔叔的...

    apktool documentation

    Apktool will work fine without running these commands, this will just cleanup abandoned files unix - mkdir -p ~/.local/share; mv ~/apktool ~/.local/share windows - move %USERPROFILE%\apktool %...

    XDA成员开发出Carrier IQ检测程序(Android)

    Starting with version 3, i will be supporting cleanup/removal from many devices. The checking part of the app will always be free here but because of the time it took me to universally code the ...

    Android Platform Developer‘s Guide.doc

    1. Confirm a Clean Installation of a Basic Linux Kernel 38 2. Modify Your Kernel Configuration to Accommodate Android Drivers 38 3. Write Drivers 38 4. Burn Images to Flash 39 5. Boot the kernel and ...

Global site tag (gtag.js) - Google Analytics