Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/nio/file/WatchService/DeleteInterference.java
38828 views
1
/*
2
* Copyright (c) 2016, Red Hat, Inc. and/or its affiliates.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*/
19
20
/**
21
* @test
22
* @bug 8153925
23
* @summary Tests potential interference between a thread creating and closing
24
* a WatchService with another thread that is deleting and re-creating the
25
* directory at around the same time. This scenario tickled a timing bug
26
* in the Windows implementation.
27
*/
28
29
import java.io.IOException;
30
import java.nio.file.DirectoryStream;
31
import java.nio.file.FileSystem;
32
import java.nio.file.FileSystems;
33
import java.nio.file.Files;
34
import java.nio.file.Path;
35
import java.nio.file.WatchService;
36
import java.util.concurrent.ExecutorService;
37
import java.util.concurrent.Executors;
38
import java.util.concurrent.Future;
39
40
import static java.nio.file.StandardWatchEventKinds.*;
41
42
public class DeleteInterference {
43
44
private static final int ITERATIONS_COUNT = 1024;
45
46
/**
47
* Execute two tasks in a thread pool. One task loops on creating and
48
* closing a WatchService, the other task deletes and re-creates the
49
* directory.
50
*/
51
public static void main(String[] args) throws Exception {
52
Path dir = Files.createTempDirectory("work");
53
ExecutorService pool = Executors.newCachedThreadPool();
54
try {
55
Future<?> task1 = pool.submit(() -> openAndCloseWatcher(dir));
56
Future<?> task2 = pool.submit(() -> deleteAndRecreateDirectory(dir));
57
task1.get();
58
task2.get();
59
} finally {
60
pool.shutdown();
61
deleteFileTree(dir);
62
}
63
}
64
65
private static void openAndCloseWatcher(Path dir) {
66
FileSystem fs = FileSystems.getDefault();
67
for (int i = 0; i < ITERATIONS_COUNT; i++) {
68
try (WatchService watcher = fs.newWatchService()) {
69
dir.register(watcher, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY);
70
} catch (IOException ioe) {
71
// ignore
72
}
73
}
74
}
75
76
private static void deleteAndRecreateDirectory(Path dir) {
77
for (int i = 0; i < ITERATIONS_COUNT; i++) {
78
try {
79
deleteFileTree(dir);
80
Path subdir = Files.createDirectories(dir.resolve("subdir"));
81
Files.createFile(subdir.resolve("test"));
82
} catch (IOException ioe) {
83
// ignore
84
}
85
}
86
}
87
88
private static void deleteFileTree(Path file) {
89
try {
90
if (Files.isDirectory(file)) {
91
try (DirectoryStream<Path> stream = Files.newDirectoryStream(file)) {
92
for (Path pa : stream) {
93
deleteFileTree(pa);
94
}
95
}
96
}
97
Files.delete(file);
98
} catch (IOException ioe) {
99
// ignore
100
}
101
}
102
}
103
104