|
1 /* |
|
2 * Copyright 2008-2009 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
20 * CA 95054 USA or visit www.sun.com if you need additional information or |
|
21 * have any questions. |
|
22 */ |
|
23 |
|
24 import java.nio.channels.AsynchronousChannelGroup; |
|
25 import java.util.concurrent.*; |
|
26 |
|
27 /** |
|
28 * Test that arbitrary tasks can be submitted to a channel group's thread pool. |
|
29 */ |
|
30 |
|
31 public 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 |
|
41 try { |
|
42 // execute simple tasks |
|
43 testSimpleTask(group1); |
|
44 testSimpleTask(group2); |
|
45 |
|
46 // install security manager and test again |
|
47 System.setSecurityManager( new SecurityManager() ); |
|
48 testSimpleTask(group1); |
|
49 testSimpleTask(group2); |
|
50 |
|
51 // attempt to execute tasks that run with only frames from boot |
|
52 // class loader on the stack. |
|
53 testAttackingTask(group1); |
|
54 testAttackingTask(group2); |
|
55 } finally { |
|
56 group1.shutdown(); |
|
57 group2.shutdown(); |
|
58 } |
|
59 } |
|
60 |
|
61 static void testSimpleTask(AsynchronousChannelGroup group) throws Exception { |
|
62 Executor executor = (Executor)group; |
|
63 final CountDownLatch latch = new CountDownLatch(1); |
|
64 executor.execute(new Runnable() { |
|
65 public void run() { |
|
66 latch.countDown(); |
|
67 } |
|
68 }); |
|
69 latch.await(); |
|
70 } |
|
71 |
|
72 static void testAttackingTask(AsynchronousChannelGroup group) throws Exception { |
|
73 Executor executor = (Executor)group; |
|
74 Attack task = new Attack(); |
|
75 executor.execute(task); |
|
76 task.waitUntilDone(); |
|
77 if (!task.failedDueToSecurityException()) |
|
78 throw new RuntimeException("SecurityException expected"); |
|
79 } |
|
80 |
|
81 } |