Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/nio/channels/FileChannel/ReleaseOnCloseDeadlock.java
41154 views
1
/*
2
* Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
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
/* @test
25
* @bug 6543863 6842687
26
* @summary Try to cause a deadlock between (Asynchronous)FileChannel.close
27
* and FileLock.release
28
*/
29
30
import java.io.*;
31
import java.nio.file.Path;
32
import static java.nio.file.StandardOpenOption.*;
33
import java.nio.channels.*;
34
import java.util.concurrent.*;
35
36
public class ReleaseOnCloseDeadlock {
37
private static final int LOCK_COUNT = 1024;
38
39
public static void main(String[] args) throws IOException {
40
File blah = File.createTempFile("blah", null);
41
blah.deleteOnExit();
42
try {
43
for (int i=0; i<100; i++) {
44
test(blah.toPath());
45
}
46
} finally {
47
blah.delete();
48
}
49
}
50
51
static void test(Path file) throws IOException {
52
FileLock[] locks = new FileLock[LOCK_COUNT];
53
54
FileChannel fc = FileChannel.open(file, READ, WRITE);
55
for (int i=0; i<LOCK_COUNT; i++) {
56
locks[i] = fc.lock(i, 1, true);
57
}
58
tryToDeadlock(fc, locks);
59
60
AsynchronousFileChannel ch = AsynchronousFileChannel.open(file, READ, WRITE);
61
for (int i=0; i<LOCK_COUNT; i++) {
62
try {
63
locks[i] = ch.lock(i, 1, true).get();
64
} catch (InterruptedException x) {
65
throw new RuntimeException(x);
66
} catch (ExecutionException x) {
67
throw new RuntimeException(x);
68
}
69
}
70
tryToDeadlock(ch, locks);
71
}
72
73
static void tryToDeadlock(final Channel channel, FileLock[] locks)
74
throws IOException
75
{
76
// start thread to close the file (and invalidate the locks)
77
Thread closer = new Thread(
78
new Runnable() {
79
public void run() {
80
try {
81
channel.close();
82
} catch (IOException ignore) {
83
ignore.printStackTrace();
84
}
85
}});
86
closer.start();
87
88
// release the locks explicitly
89
for (int i=0; i<locks.length; i++) {
90
try {
91
locks[i].release();
92
} catch (ClosedChannelException ignore) { }
93
}
94
95
// we are done when closer has terminated
96
while (closer.isAlive()) {
97
try {
98
closer.join();
99
} catch (InterruptedException ignore) { }
100
}
101
}
102
}
103
104