AsExecutor.java revision 6073:cea72c2bf071
1/* 2 * Copyright (c) 2008, 2012, 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 24import java.nio.channels.AsynchronousChannelGroup; 25import java.util.concurrent.*; 26 27/** 28 * Test that arbitrary tasks can be submitted to a channel group's thread pool. 29 */ 30 31public class AsExecutor { 32 33 public static void main(String[] args) throws Exception { 34 // create channel groups 35 ThreadFactory factory = new PrivilegedThreadFactory(); 36 AsynchronousChannelGroup group1 = AsynchronousChannelGroup 37 .withFixedThreadPool(5, factory); 38 AsynchronousChannelGroup group2 = AsynchronousChannelGroup 39 .withCachedThreadPool(Executors.newCachedThreadPool(factory), 0); 40 AsynchronousChannelGroup group3 = AsynchronousChannelGroup 41 .withThreadPool(Executors.newFixedThreadPool(10, factory)); 42 43 try { 44 // execute simple tasks 45 testSimpleTask(group1); 46 testSimpleTask(group2); 47 testSimpleTask(group3); 48 49 // install security manager and test again 50 System.setSecurityManager( new SecurityManager() ); 51 testSimpleTask(group1); 52 testSimpleTask(group2); 53 testSimpleTask(group3); 54 55 // attempt to execute tasks that run with only frames from boot 56 // class loader on the stack. 57 testAttackingTask(group1); 58 testAttackingTask(group2); 59 testAttackingTask(group3); 60 } finally { 61 group1.shutdown(); 62 group2.shutdown(); 63 group3.shutdown(); 64 } 65 } 66 67 static void testSimpleTask(AsynchronousChannelGroup group) throws Exception { 68 Executor executor = (Executor)group; 69 final CountDownLatch latch = new CountDownLatch(1); 70 executor.execute(new Runnable() { 71 public void run() { 72 latch.countDown(); 73 } 74 }); 75 latch.await(); 76 } 77 78 static void testAttackingTask(AsynchronousChannelGroup group) throws Exception { 79 Executor executor = (Executor)group; 80 Attack task = new Attack(); 81 executor.execute(task); 82 task.waitUntilDone(); 83 if (!task.failedDueToSecurityException()) 84 throw new RuntimeException("SecurityException expected"); 85 } 86 87} 88