Path: blob/master/test/jdk/java/nio/channels/AsynchronousChannelGroup/AsExecutor.java
41152 views
/*1* Copyright (c) 2008, 2017, 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/*24* @test25* @bug 460727226* @summary tests tasks can be submitted to a channel group's thread pool.27* @library /test/lib bootlib28* @build PrivilegedThreadFactory Attack29* jdk.test.lib.util.JarUtils30* @run driver SetupJar31* @run main/othervm -Xbootclasspath/a:privileged.jar -Djava.security.manager=allow AsExecutor32*/3334import java.nio.channels.AsynchronousChannelGroup;35import java.util.concurrent.CountDownLatch;36import java.util.concurrent.Executor;37import java.util.concurrent.Executors;38import java.util.concurrent.ThreadFactory;3940public class AsExecutor {4142public static void main(String[] args) throws Exception {43// create channel groups44ThreadFactory factory = new PrivilegedThreadFactory();45AsynchronousChannelGroup group1 = AsynchronousChannelGroup46.withFixedThreadPool(5, factory);47AsynchronousChannelGroup group2 = AsynchronousChannelGroup48.withCachedThreadPool(Executors.newCachedThreadPool(factory), 0);49AsynchronousChannelGroup group3 = AsynchronousChannelGroup50.withThreadPool(Executors.newFixedThreadPool(10, factory));5152try {53// execute simple tasks54testSimpleTask(group1);55testSimpleTask(group2);56testSimpleTask(group3);5758// install security manager and test again59System.setSecurityManager( new SecurityManager() );60testSimpleTask(group1);61testSimpleTask(group2);62testSimpleTask(group3);6364// attempt to execute tasks that run with only frames from boot65// class loader on the stack.66testAttackingTask(group1);67testAttackingTask(group2);68testAttackingTask(group3);69} finally {70group1.shutdown();71group2.shutdown();72group3.shutdown();73}74}7576static void testSimpleTask(AsynchronousChannelGroup group) throws Exception {77Executor executor = (Executor)group;78final CountDownLatch latch = new CountDownLatch(1);79executor.execute(new Runnable() {80public void run() {81latch.countDown();82}83});84latch.await();85}8687static void testAttackingTask(AsynchronousChannelGroup group) throws Exception {88Executor executor = (Executor)group;89Attack task = new Attack();90executor.execute(task);91task.waitUntilDone();92if (!task.failedDueToSecurityException())93throw new RuntimeException("SecurityException expected");94}9596}979899