{"id":202266,"date":"2020-10-14T08:34:52","date_gmt":"2020-10-14T00:34:52","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=202266"},"modified":"2020-09-29T09:35:31","modified_gmt":"2020-09-29T01:35:31","slug":"java-six-linux","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/java-six-linux.html","title":{"rendered":"\u5bf9\u6bd4\u5206\u6790Java\u53cd\u5c04\u83b7\u53d6\u5b9e\u4f8b\u7684\u901f\u5ea6"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u4ecb\u7ecd\u4e86Java\u53cd\u5c04\u83b7\u53d6\u5b9e\u4f8b\u7684\u901f\u5ea6\u5bf9\u6bd4\u5206\u6790\uff0c\u5177\u6709\u5f88\u597d\u7684\u53c2\u8003\u4ef7\u503c\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u6709\u6240\u5e2e\u52a9\u3002\u4e00\u8d77\u8ddf\u968f\u5c0f\u7f16\u8fc7\u6765\u770b\u770b\u5427<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\u4e4b\u524d\u4ee3\u7801\u6709\u4e00\u4e2a\u903b\u8f91\uff0c\u662f\u5728\u521d\u59cb\u5316\u65f6\u8bfb\u53d6\u67d0\u4e2a\u5305\u4e0b\u7684\u6240\u6709class\u6587\u4ef6\uff0c\u653e\u5165\u5230\u4e00\u4e2aHashMap\u91cc\u3002\u4ee3\u7801\u8fd0\u884c\u8fc7\u7a0b\u4e2d\uff0c\u901a\u8fc7Key\u83b7\u53d6\u5230\u5bf9\u5e94class\u7684\u5168\u8def\u5f84\u540d\uff0c\u6700\u540e\u901a\u8fc7Class.forName(className).getDeclaredConstructor().newInstance()\u83b7\u53d6\u5b9e\u4f8b\u5bf9\u8c61\u3002<\/p>\n

\u540e\u6765\u540c\u4e8b\u770b\u5230\u4e86\u4ee3\u7801\uff0c\u5bf9\u8fd9\u4e2aHashMap\u91cc\u5b58\u50a8\u65b9\u5f0f\u63d0\u51fa\u4e86\u5efa\u8bae\uff0c\u4e4b\u524d\u7684Map\u662f\u5b8c\u5168\u53ef\u4ee5\u6539\u6210<\/p>\n

\u540e\u6765\u6211\u6d4b\u8bd5\u4e86\u4e00\u4e0b\u4e24\u8005\u5b9e\u4f8b\u5316\u4e00\u4e2a\u5bf9\u8c61\u7684\u901f\u5ea6\uff1a<\/strong><\/span><\/div>\n
\r\npublic static void main(String[] args) {\r\n  try {\r\n    int MAX = 100000;\r\n    for (int count = 0; count < 50; count++) {\r\n      System.out.println(\"====\u7b2c\" + count+\"\u6b21\");\r\n \r\n      long s1 = System.currentTimeMillis();\r\n      for (int i = 0; i < MAX; i++) {\r\n        Person o = (Person)Class.forName(\"com.qingtai.domin.Person\").newInstance();\r\n      }\r\n      long e1 = System.currentTimeMillis();\r\n      System.out.println(\"1_duration:\" + (e1 - s1));\r\n \r\n      long s2 = System.currentTimeMillis();\r\n      Class clazz = Class.forName(\"com.qingtai.domin.Person\");\r\n      for (int i = 0; i < MAX; i++) {\r\n        Person person = (Person) clazz.newInstance();\r\n      }\r\n      long e2 = System.currentTimeMillis();\r\n      System.out.println(\"2_duration:\" + (e2 - s2));\r\n    }\r\n  } catch (Exception e) {\r\n    e.printStackTrace();\r\n  }\r\n}<\/pre>\n

\u8f93\u51fa\uff1a<\/p>\n

====\u7b2c39\u6b21 1_duration:72 2_duration:3 ====\u7b2c40\u6b21 1_duration:79 2_duration:12 ====\u7b2c41\u6b21 1_duration:92 2_duration:8 ====\u7b2c42\u6b21 1_duration:80 2_duration:5<\/pre>\n
\u7ed3\u8bba\uff1a
\n<\/strong><\/span><\/div>\n

Map\u7684value\u4e0d\u5b58\u50a8\u5168\u8def\u5f84\u540d\uff0c\u5728\u521d\u59cb\u5316\u7684\u65f6\u5019Map\u7684value\u76f4\u63a5\u5b58\u50a8Class\u5bf9\u8c61\uff0c\u5728\u53cd\u5c04\u8fc7\u7a0b\u4e2d\u901f\u5ea6\u63d0\u5347\u5f88\u5927\u3002<\/p>\n

\u8865\u5145\u77e5\u8bc6\uff1ajava\u53cd\u5c04\u83b7\u53d6\u7c7b\u5b9e\u4f8b\u5e76\u8c03\u7528\u79c1\u6709\u65b9\u6cd5<\/strong><\/span><\/div>\n

\u6211\u5c31\u5e9f\u8bdd\u4e0d\u591a\u8bf4\u4e86\uff0c\u5927\u5bb6\u8fd8\u662f\u76f4\u63a5\u770b\u4ee3\u7801\u5427~<\/p>\n

public class TestReflect {\/\/\u6d4b\u8bd5\u7c7b\r\n  \r\n public void mPublic() {\/\/\u8bbf\u95ee\u6743\u9650\u6700\u5927\r\n System.out.println(\"public run\");\r\n }\r\n  \r\n protected void mProtected() {\/\/\u540c\u5305\u4e0b\u624d\u80fd\u8bbf\u95ee\uff08\u5b9e\u9a8c\u5bf9\u8c61\uff09\r\n System.out.println(\"protected run\");\r\n }\r\n  \r\n private void mPrivate() {\/\/\u53ea\u6709\u672c\u7c7b\u4e2d\u624d\u80fd\u8bbf\u95ee\uff08\u5b9e\u9a8c\u5bf9\u8c61\uff09\r\n System.out.println(\"private run\");\r\n } \r\n}<\/pre>\n
public static void main(String[] args) throws Exception {\r\nClass class1 = null;\r\n\/\/ \u53cd\u5c04\u83b7\u53d6\u7c7b\u5b9e\u4f8b\uff0c\u7528\u7684\u6700\u591a\u7684\u5c31\u662fjdbc\u83b7\u53d6\u9a71\u52a8\u7684\u65f6\u5019\u5c31\u662f\u7528Class.forName(\"xxx\");\r\n\/\/ \u4e00\u822c\u91c7\u7528\u8fd9\u79cd\u5f62\u5f0f\r\nclass1 = Class.forName(\"com.xxx.TestReflect\");\r\n\/\/ class1 = new TestReflect().getClass();\r\n\/\/ class1 = TestReflect.class;\r\n \r\n\/\/ \u7c7b\u5b9e\u4f8b\u5316\uff0c\u5230\u8fd9\u91cc\u5c31\u53ef\u4ee5\u8bbf\u95eeTestReflect\u7c7b\u7684public\u5c5e\u6027\u7684\u6210\u5458\u65b9\u6cd5\u548c\u6210\u5458\u53d8\u91cf\u4e86\r\nTestReflect tr = (TestReflect) class1.newInstance();\r\n \r\n\/\/ \u901a\u8fc7java.lang.Class\u7c7b\u5f97\u5230\u4e00\u4e2aMethod\u5bf9\u8c61\r\n\/\/ api\u4e2djava.lang.Class.getDeclaredMethod\u65b9\u6cd5\u4ecb\u7ecd\r\n\/\/ \u8fd4\u56de\u4e00\u4e2a Method \u5bf9\u8c61\uff0c\u8be5\u5bf9\u8c61\u53cd\u6620\u6b64 Class \u5bf9\u8c61\u6240\u8868\u793a\u7684\u7c7b\u6216\u63a5\u53e3\u7684\u6307\u5b9a\u5df2\u58f0\u660e\u65b9\u6cd5\u3002\r\n  Method method = class1.getDeclaredMethod(\"mPrivate\");\r\n  Method method1 = class1.getDeclaredMethod(\"mProtected\");\r\n   \r\n  \/\/\u5c06\u6b64\u5bf9\u8c61\u7684 accessible \u6807\u5fd7\u8bbe\u7f6e\u4e3a\u6307\u793a\u7684\u5e03\u5c14\u503c\u3002\r\n\/\/\u503c\u4e3a true \u5219\u6307\u793a\u53cd\u5c04\u7684\u5bf9\u8c61\u5728\u4f7f\u7528\u65f6\u5e94\u8be5\u53d6\u6d88 Java \u8bed\u8a00\u8bbf\u95ee\u68c0\u67e5\u3002\r\n\/\/\u503c\u4e3a false \u5219\u6307\u793a\u53cd\u5c04\u7684\u5bf9\u8c61\u5e94\u8be5\u5b9e\u65bd Java \u8bed\u8a00\u8bbf\u95ee\u68c0\u67e5\u3002\r\n  method.setAccessible(true); \r\n  method1.setAccessible(true);\r\n   \r\n  \/\/ \u8c03\u7528\u8be5\u65b9\u6cd5\r\n  method.invoke(tr);\r\n  method1.invoke(tr);\r\n}<\/pre>\n

\u4ee5\u4e0a\u8fd9\u7bc7Java\u53cd\u5c04\u83b7\u53d6\u5b9e\u4f8b\u7684\u901f\u5ea6\u5bf9\u6bd4\u5206\u6790\u5c31\u662f\u5c0f\u7f16\u5206\u4eab\u7ed9\u5927\u5bb6\u7684\u5168\u90e8\u5185\u5bb9\u4e86\uff0c\u5e0c\u671b\u80fd\u7ed9\u5927\u5bb6\u4e00\u4e2a\u53c2\u8003\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

\u4e4b\u524d\u4ee3\u7801\u6709\u4e00\u4e2a\u903b\u8f91\uff0c\u662f\u5728\u521d\u59cb\u5316\u65f6\u8bfb\u53d6\u67d0\u4e2a\u5305\u4e0b\u7684\u6240\u6709class\u6587\u4ef6\uff0c\u653e\u5165\u5230\u4e00\u4e2aHashMap\u91cc\u3002\u4ee3\u7801\u8fd0\u884c\u8fc7\u7a0b\u4e2d\uff0c […]<\/p>\n","protected":false},"author":1482,"featured_media":121883,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-202266","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/202266","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1482"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=202266"}],"version-history":[{"count":5,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/202266\/revisions"}],"predecessor-version":[{"id":202274,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/202266\/revisions\/202274"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/121883"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=202266"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=202266"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=202266"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}