Path: blob/master/test/jdk/java/nio/channels/SelectionKey/AtomicAttachTest.java
41153 views
/*1* Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @bug 643622025* @summary SelectionKey.attach should atomically set and return the attachment26*/27import java.nio.channels.*;28import java.util.concurrent.atomic.*;2930public class AtomicAttachTest {31public static void main(String[] args) throws Exception {32Selector selector = Selector.open();33Pipe pipe = Pipe.open();34SelectableChannel channel = pipe.sink().configureBlocking(false);35final SelectionKey key = channel.register(selector, 0);36key.attach(new AtomicBoolean());3738final AtomicInteger errorCount = new AtomicInteger();3940Thread t = new Thread() {41public void run() {42AtomicBoolean att = new AtomicBoolean();43for (int i=0; i<(10*1000*1000); i++) {44att = (AtomicBoolean)key.attach(att);45// We should have exclusive ownership of att.46if (!att.compareAndSet(false, true) ||47!att.compareAndSet(true, false))48{49errorCount.incrementAndGet();50}51}52}53{54start();55run();56}57};5859t.join();6061pipe.sink().close();62pipe.source().close();63selector.close();6465int count = errorCount.get();66if (count > 0) {67throw new RuntimeException("Error count:" + count);68}69}70}717273