JAVA UTIL CLEAN

back to JAVA projects back to JAVA util projects

description
This project contains utility classes which can be used to simplify resource cleaning. Java 1.7 introduced automatic resource management and the AutoCloseable interface, which is perfect for simple use cases. But sometimes, the resource is not closed by the same method (or even thread) that opens it. Cleaner has a static instance and can also be instantiated in case more than one cleaner is needed for several sets of resources.

example of use

 * for application GUIs that use threads for animations, periodic tasks in background ... closing the main frame will not exit the jvm since there are still threads alive. Add a cleaner for each of your thread running in background :

Cleaner.getInstance.addCleanerFor(new Runnable {       @Override        public void run { /* clean */ }    });

frame.addWindowListener(new WindowAdapter {       @Override        public void windowClosing(final WindowEvent e) {            Cleaner.getInstance.clean;        }    });
 * Swing listener for cleaning when closing main frame :


 * JavaFX listener for cleaning when closing application :

stage.setOnHiding(new EventHandler {       @Override        public void handle(final WindowEvent arg0) {            Cleaner.getInstance.clean;        }    });


 * CloseableThread from util-thread can be used for repetitive tasks performed in background to simplify this cleaning process :

CloseableThread workerThread = new CloseableThread { protected void performTask { /* do something */ }; };   workerThread.start; Cleaner.getInstance.addCleanerFor(workerThread);

dependencies

 * SLF4J API 1.7.7 for logging

version history

 * 0.2.2 2014/08/31
 * added basic junit tests
 * added log message for removeCleaner
 * improved logs and javadoc for CleanerThread
 * updated source/target version for java 8
 * update SLF4J version to 1.7.7
 * 0.2.1 2013/04/30
 * added log messages
 * externalized CleanerThread and made it public
 * 0.2.0 2013/04/20
 * surronded each close with try / catch
 * added javadoc and log messages
 * synchronized access to cleaner list
 * added removeCleaner method
 * addCleanerFor now returns the cleaner (to be able to call remove)
 * renamed addCleaner to addCleanerThread
 * removed Swing CloseableTimer
 * 0.1 2012/11/02 first usable version

next version

 * 0.3.0
 * TODO
 * provide several ways to handle errors
 * provide a way to be notified when all cleaner threads have finished (with a timeout)
 * add more tests

back to JAVA util projects back to JAVA projects