本文整理匯總了Java中java.util.concurrent.Executors.privilegedCallable方法的典型用法代碼示例。如果您正苦於以下問題:Java Executors.privilegedCallable方法的具體用法?Java Executors.privilegedCallable怎麽用?Java Executors.privilegedCallable使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.util.concurrent.Executors
的用法示例。
在下文中一共展示了Executors.privilegedCallable方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: testPrivilegedCallableWithNoPrivs
import java.util.concurrent.Executors; //導入方法依賴的package包/類
/**
* Without permissions, calling privilegedCallable throws ACE
*/
public void testPrivilegedCallableWithNoPrivs() throws Exception {
// Avoid classloader-related SecurityExceptions in swingui.TestRunner
Executors.privilegedCallable(new CheckCCL());
Runnable r = new CheckedRunnable() {
public void realRun() throws Exception {
if (System.getSecurityManager() == null)
return;
Callable task = Executors.privilegedCallable(new CheckCCL());
try {
task.call();
shouldThrow();
} catch (AccessControlException success) {}
}};
runWithoutPermissions(r);
// It seems rather difficult to test that the
// AccessControlContext of the privilegedCallable is used
// instead of its caller. Below is a failed attempt to do
// that, which does not work because the AccessController
// cannot capture the internal state of the current Policy.
// It would be much more work to differentiate based on,
// e.g. CodeSource.
// final AccessControlContext[] noprivAcc = new AccessControlContext[1];
// final Callable[] task = new Callable[1];
// runWithPermissions
// (new CheckedRunnable() {
// public void realRun() {
// if (System.getSecurityManager() == null)
// return;
// noprivAcc[0] = AccessController.getContext();
// task[0] = Executors.privilegedCallable(new CheckCCL());
// try {
// AccessController.doPrivileged(new PrivilegedAction<Void>() {
// public Void run() {
// checkCCL();
// return null;
// }}, noprivAcc[0]);
// shouldThrow();
// } catch (AccessControlException success) {}
// }});
// runWithPermissions
// (new CheckedRunnable() {
// public void realRun() throws Exception {
// if (System.getSecurityManager() == null)
// return;
// // Verify that we have an underprivileged ACC
// try {
// AccessController.doPrivileged(new PrivilegedAction<Void>() {
// public Void run() {
// checkCCL();
// return null;
// }}, noprivAcc[0]);
// shouldThrow();
// } catch (AccessControlException success) {}
// try {
// task[0].call();
// shouldThrow();
// } catch (AccessControlException success) {}
// }},
// new RuntimePermission("getClassLoader"),
// new RuntimePermission("setContextClassLoader"));
}