Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/nio/channels/AsynchronousFileChannel/CustomThreadPool.java
41153 views
1
/*
2
* Copyright (c) 2008, 2009, 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 4607272 6842687
26
* @summary Unit test for java.nio.channels.AsynchronousFileChannel
27
* @build CustomThreadPool MyThreadFactory
28
* @run main/othervm -Djava.nio.channels.DefaultThreadPool.threadFactory=MyThreadFactory CustomThreadPool
29
*/
30
31
import java.io.File;
32
import static java.nio.file.StandardOpenOption.*;
33
import java.nio.ByteBuffer;
34
import java.nio.channels.*;
35
import java.util.concurrent.atomic.AtomicReference;
36
37
public class CustomThreadPool {
38
39
public static void main(String[] args) throws Exception {
40
File blah = File.createTempFile("blah", null);
41
blah.deleteOnExit();
42
AsynchronousFileChannel ch =
43
AsynchronousFileChannel.open(blah.toPath(), READ, WRITE);
44
ByteBuffer src = ByteBuffer.wrap("Scooby Snacks".getBytes());
45
46
final AtomicReference<Thread> invoker = new AtomicReference<Thread>();
47
ch.write(src, 0, invoker,
48
new CompletionHandler<Integer,AtomicReference<Thread>>() {
49
public void completed(Integer result, AtomicReference<Thread> invoker) {
50
invoker.set(Thread.currentThread());
51
}
52
public void failed(Throwable exc, AtomicReference<Thread> invoker) {
53
}
54
});
55
Thread t;
56
while ((t = invoker.get()) == null) {
57
Thread.sleep(100);
58
}
59
ch.close();
60
61
// check handler was run by known thread
62
if (!MyThreadFactory.created(t))
63
throw new RuntimeException("Handler invoked by unknown thread");
64
}
65
}
66
67