Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/nio/file/WatchService/DeleteInterference.java
41153 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
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
/**
25
* @test
26
* @bug 8153925
27
* @summary Tests potential interference between a thread creating and closing
28
* a WatchService with another thread that is deleting and re-creating the
29
* directory at around the same time. This scenario tickled a timing bug
30
* in the Windows implementation.
31
*/
32
33
import java.io.IOException;
34
import java.nio.file.DirectoryStream;
35
import java.nio.file.FileSystem;
36
import java.nio.file.FileSystems;
37
import java.nio.file.Files;
38
import java.nio.file.Path;
39
import java.nio.file.Paths;
40
import java.nio.file.WatchService;
41
import java.util.concurrent.ExecutorService;
42
import java.util.concurrent.Executors;
43
import java.util.concurrent.Future;
44
45
import static java.lang.System.out;
46
import static java.nio.file.StandardWatchEventKinds.*;
47
48
public class DeleteInterference {
49
50
private static final int ITERATIONS_COUNT = 1024;
51
52
/**
53
* Execute two tasks in a thread pool. One task loops on creating and
54
* closing a WatchService, the other task deletes and re-creates the
55
* directory.
56
*/
57
public static void main(String[] args) throws Exception {
58
Path testDir = Paths.get(System.getProperty("test.dir", "."));
59
Path dir = Files.createTempDirectory(testDir, "DeleteInterference");
60
ExecutorService pool = Executors.newCachedThreadPool();
61
try {
62
Future<?> task1 = pool.submit(() -> openAndCloseWatcher(dir));
63
Future<?> task2 = pool.submit(() -> deleteAndRecreateDirectory(dir));
64
task1.get();
65
task2.get();
66
} finally {
67
pool.shutdown();
68
}
69
}
70
71
private static void openAndCloseWatcher(Path dir) {
72
FileSystem fs = FileSystems.getDefault();
73
for (int i = 0; i < ITERATIONS_COUNT; i++) {
74
out.printf("open %d begin%n", i);
75
try (WatchService watcher = fs.newWatchService()) {
76
dir.register(watcher, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY);
77
} catch (IOException ioe) {
78
// ignore
79
} finally {
80
out.printf("open %d end%n", i);
81
}
82
}
83
}
84
85
private static void deleteAndRecreateDirectory(Path dir) {
86
for (int i = 0; i < ITERATIONS_COUNT; i++) {
87
out.printf("del %d begin%n", i);
88
try {
89
deleteFileTree(dir);
90
Path subdir = Files.createDirectories(dir.resolve("subdir"));
91
Files.createFile(subdir.resolve("test"));
92
} catch (IOException ioe) {
93
// ignore
94
} finally {
95
out.printf("del %d end%n", i);
96
}
97
}
98
}
99
100
private static void deleteFileTree(Path file) {
101
try {
102
if (Files.isDirectory(file)) {
103
try (DirectoryStream<Path> stream = Files.newDirectoryStream(file)) {
104
for (Path pa : stream) {
105
deleteFileTree(pa);
106
}
107
}
108
}
109
Files.delete(file);
110
} catch (IOException ioe) {
111
// ignore
112
}
113
}
114
}
115
116