Identity.java revision 2546
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy * Copyright (c) 2008, 2009, Oracle and/or its affiliates. All rights reserved.
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy * This code is free software; you can redistribute it and/or modify it
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy * under the terms of the GNU General Public License version 2 only, as
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy * published by the Free Software Foundation.
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy * This code is distributed in the hope that it will be useful, but WITHOUT
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy * version 2 for more details (a copy is included in the LICENSE file that
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy * accompanied this code).
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy * You should have received a copy of the GNU General Public License version
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy * 2 along with this work; if not, write to the Free Software Foundation,
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy * or visit www.oracle.com if you need additional information or have any
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy * @bug 4607272 6842687
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy * @summary Unit test for AsynchronousChannelGroup
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy * Tests that the completion handler is invoked by a thread with
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy * the expected identity.
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy static final CountDownLatch done = new CountDownLatch(1);
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy static final AtomicBoolean failed = new AtomicBoolean(false);
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy // thread-local identifies the thread
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy private static final ThreadLocal<Integer> myGroup =
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy @Override protected Integer initialValue() {
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy // creates a ThreadFactory that constructs groups with the given identity
4b5c8e93cab28d3c65ba9d407fd8f46e3be1db1cMatthew Ahrens static final ThreadFactory createThreadFactory(final int groupId) {
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy public Thread newThread(final Runnable r) {
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy public void run() {
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy public static void main(String[] args) throws Exception {
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy // create listener to accept connections
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy final AsynchronousServerSocketChannel listener =
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy listener.accept((Void)null, new CompletionHandler<AsynchronousSocketChannel,Void>() {
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy public void completed(final AsynchronousSocketChannel ch, Void att) {
d583b39bfb4e2571d3e41097c5c357ffe353ad45John Wren Kennedy final ByteBuffer buf = ByteBuffer.allocate(100);
return buf;