刚发现 java.lang.SecurityManager 的好用之处就发现
@Deprecated(since="17", forRemoval=true)
public class SecurityManager {
从java17开始@Deprecated , 将要被移除
Why?Why?Why?
替代品是什么?
以下是对其获取当前类的性能的测试
测试代码👇
package securityManager;
public class GetClassContext速度测试2206021812 {
static Class<?> cls=null;
public static void test(int amount) {
long beginning = System.currentTimeMillis();
for(int c=0; c<amount; c++) {
cls = new SecurityManager() {Class<?> n(){return this.getClassContext()[1];}}.n();
}
long t2 = System.currentTimeMillis();
long cost1 = t2 - beginning;
System.out.println("执行 "+amount/10000+" 万次,用时 "+ cost1 +" 毫秒");
}
static int w = 10000;
static int iar[] = new int[] {
w,w,w,w,w,w,
w,20000,30000,40000,50000,60000,70000,80000,90000,
w,20000,30000,40000,50000,60000,70000,80000,90000,
10*w, 20*w, 30*w, 40*w, 50*w, 60*2, 70*w, 80*w, 90*w,
100*w, 200*w, 300*w
};
public static void main(String...arguments) {
for(int max : iar)test(max);
}
}
结果👇
执行 1 万次,用时 32 毫秒
执行 1 万次,用时 4 毫秒
执行 1 万次,用时 4 毫秒
执行 1 万次,用时 4 毫秒
执行 1 万次,用时 4 毫秒
执行 1 万次,用时 4 毫秒
执行 1 万次,用时 3 毫秒
执行 2 万次,用时 6 毫秒
执行 3 万次,用时 10 毫秒
执行 4 万次,用时 13 毫秒
执行 5 万次,用时 16 毫秒
执行 6 万次,用时 20 毫秒
执行 7 万次,用时 23 毫秒
执行 8 万次,用时 26 毫秒
执行 9 万次,用时 29 毫秒
执行 1 万次,用时 4 毫秒
执行 2 万次,用时 6 毫秒
执行 3 万次,用时 10 毫秒
执行 4 万次,用时 12 毫秒
执行 5 万次,用时 16 毫秒
执行 6 万次,用时 19 毫秒
执行 7 万次,用时 24 毫秒
执行 8 万次,用时 26 毫秒
执行 9 万次,用时 30 毫秒
执行 10 万次,用时 34 毫秒
执行 20 万次,用时 62 毫秒
执行 30 万次,用时 94 毫秒
执行 40 万次,用时 121 毫秒
执行 50 万次,用时 156 毫秒
执行 0 万次,用时 0 毫秒
执行 70 万次,用时 222 毫秒
执行 80 万次,用时 255 毫秒
执行 90 万次,用时 273 毫秒
执行 100 万次,用时 328 毫秒
执行 200 万次,用时 660 毫秒
执行 300 万次,用时 979 毫秒