当前位置: 首页>>代码示例>>Java>>正文


Java WatchService.take方法代码示例

本文整理汇总了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();
}
 
开发者ID:sworisbreathing,项目名称:jpathwatch,代码行数:59,代码来源:JDK7Test.java

示例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();
}
 
开发者ID:sworisbreathing,项目名称:jpathwatch,代码行数:44,代码来源:JDK7Test.java


注:本文中的name.pachler.nio.file.WatchService.take方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。