jdk/test/java/nio/channels/AsynchronousFileChannel/CustomThreadPool.java
changeset 2057 3acf8e5e2ca0
child 3632 399359a027de
equal deleted inserted replaced
2056:115e09b7a004 2057:3acf8e5e2ca0
       
     1 /*
       
     2  * Copyright 2008-2009 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    20  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    21  * have any questions.
       
    22  */
       
    23 
       
    24 /* @test
       
    25  * @bug 4607272
       
    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                 public void cancelled(AtomicReference<Thread> invoker) {
       
    55                 }
       
    56             });
       
    57         Thread t;
       
    58         while ((t = invoker.get()) == null) {
       
    59             Thread.sleep(100);
       
    60         }
       
    61         ch.close();
       
    62 
       
    63         // check handler was run by known thread
       
    64         if (!MyThreadFactory.created(t))
       
    65             throw new RuntimeException("Handler invoked by unknown thread");
       
    66     }
       
    67 }