4

アプリケーションでを使用しWatchServiceています。Windowsアプリケーションを環境で実行すると、アプリケーションはCPU. 私のLinuxサーバーで同じアプリケーションを実行すると、CPU. WatchServiceスレッドが無効になると、はCPU正常に戻ります。

で使用CentOS 5.9していOpenJDK-1.7.0_x86_64ます。

スレッドは次のとおりです。

private static void startDirectoryWatcher() {
    if (thWatcherM == null) {
        thWatcherM = new Thread(new Runnable() {
            @Override
            public void run() {
                if (mediaMode == MediaMode.Directory && !exit) {

                    File music = new File(path);

                    WatchService watcherM = null;

                    watcherM = music.toPath().getFileSystem().newWatchService();
                    music.toPath().register(watcherM, StandardWatchEventKinds.ENTRY_CREATE);

                    while (!exit) {
                        Thread.sleep(50);
                        if (watcherM != null) {
                            WatchKey watchKey = watcherM.take();

                            List<WatchEvent<?>> events = watchKey
                                    .pollEvents();
                            for (WatchEvent<?> event : events) {
                                if (event.kind() == StandardWatchEventKinds.ENTRY_CREATE) {
                                    System.out.println(event.context().toString());
                                }
                            }

                            if (!watchKey.reset()) {
                                break;
                            }
                        }
                    }

                    if (watcherM != null) {
                        watcherM.close();
                    }

                }
            }
        });
        thWatcherM.setName("Dir-Watcher-M");
        thWatcherM.start();
    }
}

の100%を使用しているのはなぜCPUですか?

4

1 に答える 1