CustomThreadPool.java revision 1580
893N/A/*
893N/A * Copyright 2008-2009 Sun Microsystems, Inc. All Rights Reserved.
893N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
893N/A *
893N/A * This code is free software; you can redistribute it and/or modify it
893N/A * under the terms of the GNU General Public License version 2 only, as
893N/A * published by the Free Software Foundation.
893N/A *
893N/A * This code is distributed in the hope that it will be useful, but WITHOUT
893N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
893N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
893N/A * version 2 for more details (a copy is included in the LICENSE file that
893N/A * accompanied this code).
893N/A *
893N/A * You should have received a copy of the GNU General Public License version
893N/A * 2 along with this work; if not, write to the Free Software Foundation,
893N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
893N/A *
893N/A * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
893N/A * CA 95054 USA or visit www.sun.com if you need additional information or
893N/A * have any questions.
893N/A */
893N/A
893N/A/* @test
1580N/A * @bug 4607272 6842687
893N/A * @summary Unit test for java.nio.channels.AsynchronousFileChannel
893N/A * @build CustomThreadPool MyThreadFactory
893N/A * @run main/othervm -Djava.nio.channels.DefaultThreadPool.threadFactory=MyThreadFactory CustomThreadPool
893N/A */
893N/A
893N/Aimport java.io.File;
893N/Aimport static java.nio.file.StandardOpenOption.*;
893N/Aimport java.nio.ByteBuffer;
893N/Aimport java.nio.channels.*;
893N/Aimport java.util.concurrent.atomic.AtomicReference;
893N/A
893N/Apublic class CustomThreadPool {
893N/A
893N/A public static void main(String[] args) throws Exception {
893N/A File blah = File.createTempFile("blah", null);
893N/A blah.deleteOnExit();
893N/A AsynchronousFileChannel ch =
893N/A AsynchronousFileChannel.open(blah.toPath(), READ, WRITE);
893N/A ByteBuffer src = ByteBuffer.wrap("Scooby Snacks".getBytes());
893N/A
893N/A final AtomicReference<Thread> invoker = new AtomicReference<Thread>();
893N/A ch.write(src, 0, invoker,
893N/A new CompletionHandler<Integer,AtomicReference<Thread>>() {
893N/A public void completed(Integer result, AtomicReference<Thread> invoker) {
893N/A invoker.set(Thread.currentThread());
893N/A }
893N/A public void failed(Throwable exc, AtomicReference<Thread> invoker) {
893N/A }
893N/A });
893N/A Thread t;
893N/A while ((t = invoker.get()) == null) {
893N/A Thread.sleep(100);
893N/A }
893N/A ch.close();
893N/A
893N/A // check handler was run by known thread
893N/A if (!MyThreadFactory.created(t))
893N/A throw new RuntimeException("Handler invoked by unknown thread");
893N/A }
893N/A}