Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/nio/channels/AsynchronousChannelGroup/AsExecutor.java
38821 views
/*1* Copyright (c) 2008, 2012, 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*/2223import java.nio.channels.AsynchronousChannelGroup;24import java.util.concurrent.*;2526/**27* Test that arbitrary tasks can be submitted to a channel group's thread pool.28*/2930public class AsExecutor {3132public static void main(String[] args) throws Exception {33// create channel groups34ThreadFactory factory = new PrivilegedThreadFactory();35AsynchronousChannelGroup group1 = AsynchronousChannelGroup36.withFixedThreadPool(5, factory);37AsynchronousChannelGroup group2 = AsynchronousChannelGroup38.withCachedThreadPool(Executors.newCachedThreadPool(factory), 0);39AsynchronousChannelGroup group3 = AsynchronousChannelGroup40.withThreadPool(Executors.newFixedThreadPool(10, factory));4142try {43// execute simple tasks44testSimpleTask(group1);45testSimpleTask(group2);46testSimpleTask(group3);4748// install security manager and test again49System.setSecurityManager( new SecurityManager() );50testSimpleTask(group1);51testSimpleTask(group2);52testSimpleTask(group3);5354// attempt to execute tasks that run with only frames from boot55// class loader on the stack.56testAttackingTask(group1);57testAttackingTask(group2);58testAttackingTask(group3);59} finally {60group1.shutdown();61group2.shutdown();62group3.shutdown();63}64}6566static void testSimpleTask(AsynchronousChannelGroup group) throws Exception {67Executor executor = (Executor)group;68final CountDownLatch latch = new CountDownLatch(1);69executor.execute(new Runnable() {70public void run() {71latch.countDown();72}73});74latch.await();75}7677static void testAttackingTask(AsynchronousChannelGroup group) throws Exception {78Executor executor = (Executor)group;79Attack task = new Attack();80executor.execute(task);81task.waitUntilDone();82if (!task.failedDueToSecurityException())83throw new RuntimeException("SecurityException expected");84}8586}878889