1 /* 2 * Copyright (c) 2018, 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 8201315 26 * @summary Test that channels can be registered, interest ops can changed, 27 * and keys cancelled while a selection operation is in progress. 28 */ 29 30 import java.io.IOException; 31 import java.nio.channels.Pipe; 32 import java.nio.channels.SelectionKey; 33 import java.nio.channels.Selector; 34 import java.util.concurrent.Callable; 35 import java.util.concurrent.ExecutorService; 36 import java.util.concurrent.Executors; 37 import java.util.concurrent.Future; 38 import java.util.concurrent.Phaser; 39 40 public class RegisterDuringSelect { 41 42 static Callable<Void> selectLoop(Selector sel, Phaser barrier) { 43 return new Callable<Void>() { 44 @Override 45 public Void call() throws IOException { 46 for (;;) { 47 sel.select(); 48 if (sel.isOpen()) { 49 barrier.arriveAndAwaitAdvance(); 50 } else { 51 // closed 52 return null; 53 } 54 } 55 } 56 }; 57 } 58 /** 59 * Invoke register, interestOps, and cancel concurrently with a thread 60 * doing a selection operation 61 */ 62 public static void main(String args[]) throws Exception { 63 Future<Void> result; 64 65 ExecutorService pool = Executors.newFixedThreadPool(1); 66 try (Selector sel = Selector.open()) { 67 Phaser barrier = new Phaser(2); 68 69 // submit task to do the select loop 70 result = pool.submit(selectLoop(sel, barrier)); 71 72 Pipe p = Pipe.open(); 73 try { 74 Pipe.SourceChannel sc = p.source(); 75 sc.configureBlocking(false); 76 77 System.out.println("register ..."); 78 SelectionKey key = sc.register(sel, SelectionKey.OP_READ); 79 if (!sel.keys().contains(key)) 80 throw new RuntimeException("key not in key set"); 81 sel.wakeup(); 82 barrier.arriveAndAwaitAdvance(); 83 84 System.out.println("interestOps ..."); 85 key.interestOps(0); 86 sel.wakeup(); 87 barrier.arriveAndAwaitAdvance(); 88 89 System.out.println("cancel ..."); 90 key.cancel(); 91 sel.wakeup(); 92 barrier.arriveAndAwaitAdvance(); 93 if (sel.keys().contains(key)) 94 throw new RuntimeException("key not removed from key set"); 95 96 } finally { 97 p.source().close(); 98 p.sink().close(); 99 } 100 101 } finally { 102 pool.shutdown(); 103 } 104 105 // ensure selectLoop completes without exception 106 result.get(); 107 108 } 109 }