FileScanner implements Runnable { public void registerListener(Listener listener) { listeners.add(listener); } FileScanner(File buildRoot, int interval); void registerListener(Listener listener); @Override void run();  }
@Test public void testFileScan() throws Exception { File file = new File("build"); FileScanner fileScanner = new FileScanner(file,10); AtomicInteger counter = new AtomicInteger(); counter.getAndSet(0); Listener listener = event -> counter.getAndIncrement(); fileScanner.registerListener(listener); Thread f = new Thread(fileScanner); f.start(); TimeUnit.MILLISECONDS.sleep(50); FileWriter myWriter = new FileWriter("build/test.txt"); myWriter.write("test"); myWriter.close(); TimeUnit.MILLISECONDS.sleep(50); FileUtils.deleteDir(new File("build/test.txt")); TimeUnit.MILLISECONDS.sleep(50); assertEquals(2,counter.get()); }