本文整理汇总了Java中name.pachler.nio.file.WatchService.take方法的典型用法代码示例。如果您正苦于以下问题:Java WatchService.take方法的具体用法?Java WatchService.take怎么用?Java WatchService.take使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类name.pachler.nio.file.WatchService
的用法示例。
在下文中一共展示了WatchService.take方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testOneTaker
import name.pachler.nio.file.WatchService; //导入方法依赖的package包/类
@Test
public synchronized void testOneTaker() throws IOException, InterruptedException{
WatchService watcher = Bootstrapper.newWatchService();
File dir = createTempDir("dir1");
Path path = Bootstrapper.newPath(dir);
WatchKey wk = path.register(watcher, StandardWatchEventKind.ENTRY_CREATE);
// wait for 200ms - should return no key
WatchKey key = watcher.poll(200, TimeUnit.MILLISECONDS);
assertNull(key);
File file1 = new File(dir, "file1");
file1.createNewFile();
wait(200);
key = watcher.take();
assertNotNull(key);
List<WatchEvent<?>> eventList = key.pollEvents();
assertTrue(eventList.size() == 1);
WatchEvent event = eventList.get(0);
assertEquals(event.context(), Bootstrapper.newPath(new File("file1")));
assertEquals(event.kind(), ENTRY_CREATE);
key.reset();
File file2 = new File(dir, "file2");
file2.createNewFile();
wait(200);
key = watcher.take();
assertNotNull(key);
eventList = key.pollEvents();
assertTrue(eventList.size() == 1);
event = eventList.get(0);
assertEquals(event.context(), Bootstrapper.newPath(new File("file2")));
assertEquals(event.kind(), ENTRY_CREATE);
key.reset();
// test file with single-character file name
File file3 = new File(dir, "3");
file3.createNewFile();
wait(200);
key = watcher.take();
assertNotNull(key);
eventList = key.pollEvents();
assertTrue(eventList.size() == 1);
event = eventList.get(0);
assertEquals(event.context(), Bootstrapper.newPath(new File("3")));
assertEquals(event.kind(), ENTRY_CREATE);
watcher.close();
}
示例2: testOneTakerThreaded
import name.pachler.nio.file.WatchService; //导入方法依赖的package包/类
@Test
public synchronized void testOneTakerThreaded() throws IOException, InterruptedException{
// WatchKeys for two distinct directories are created. Two threads
// are created, each calling take() once. In each observed directory,
// a single file is created.
// The expected result is that each thread receives one of the WatchKeys
// as a return value from take().
WatchService watcher = Bootstrapper.newWatchService();
SingleWatchKeyTaker swkt = new SingleWatchKeyTaker(watcher);
File dir = createTempDir("dir1");
Path path = Bootstrapper.newPath(dir);
WatchKey wk = path.register(watcher, StandardWatchEventKind.ENTRY_CREATE);
File file = new File(dir, "file1");
file.createNewFile();
file.deleteOnExit();
WatchKey key = watcher.take();
assertTrue(key != null);
boolean resetSuccessful = key.reset();
assertTrue(resetSuccessful);
swkt.start();
File file2 = new File(dir, "file2");
file2.createNewFile();
file2.deleteOnExit();
wait(200); // wait a second to give threads time to process events
// because threads each take a single watch key and terminate straight
// after, they should all be dead by now
Assert.assertFalse(swkt.isAlive());
// check that the watch keys the threads received are distinct, and
// that they were the keys that we used
Assert.assertTrue(swkt.watchKey == wk);
watcher.close();
}