{"id":198,"date":"2022-01-05T22:35:14","date_gmt":"2022-01-05T14:35:14","guid":{"rendered":"https:\/\/www.xudongvp.com\/?p=198"},"modified":"2023-04-07T09:52:43","modified_gmt":"2023-04-07T01:52:43","slug":"%e5%85%b3%e4%ba%8e%e8%8e%b7%e5%8f%96%e8%a1%8c%e5%8f%b7%e7%9a%84%e4%bb%a3%e4%bb%b7","status":"publish","type":"post","link":"https:\/\/www.xudongvp.com\/?p=198","title":{"rendered":"JAVA\u4e2d\u83b7\u53d6\u7c7b\u540d\u4e0e\u884c\u53f7\u7684\u4ee3\u4ef7"},"content":{"rendered":"\n<p>\u6211\u4eec\u5728\u4f7f\u7528Java\u7684\u65e5\u5fd7\u5e93\u8fdb\u884c\u65e5\u5fd7\u6253\u5370\u65f6\uff0c\u6709\u4e2a\u5fae\u5c0f\u7684\u529f\u80fd\uff0c\u6253\u5370\u5f53\u524d\u4ee3\u7801\u884c\u6240\u5728\u7684\u7c7b\u540d\uff0c\u65b9\u6cd5\u540d\u4ee5\u53ca\u884c\u53f7\u7b49\u4fe1\u606f\uff0c\u8fd9\u4e2a\u529f\u80fd\u662f\u5982\u4f55\u5b9e\u73b0\u7684\u5462\uff1f\u8bf4\u8d77\u6765\u8fd9\u8fd8\u662f\u4e00\u4e2a\u63a8\u52a8JDK\u53d1\u5c55\u7684\u4e8b\u60c5\uff0c\u4ece\u6700\u521d\u7684\u6ca1\u63d0\u4f9b\u76f8\u5173\u7684\u80fd\u529b\uff0c\u5230JDK1.5\u4e4b\u540e\u63d0\u4f9b<meta charset=\"utf-8\"><em>Thread.currentThread().getStackTrace()<\/em>\u65b9\u6cd5\u80fd\u591f\u65b9\u4fbf\u7684\u83b7\u53d6<meta charset=\"utf-8\">StackTraceElement\u6570\u7ec4\uff0c\u8fdb\u800c\u65b9\u4fbf\u7684\u62ff\u5230\u7c7b\u540d\u4e0e\u884c\u53f7\u7b49\u4fe1\u606f\uff0c\u4e5f\u662f\u975e\u5e38\u597d\u7684\u4e00\u4e2a\u8fdb\u5316\u4f8b\u5b50\u3002\u4f46\u662f\u6211\u4eec\u8fd9\u91cc\u5173\u6ce8\u7684\u662f\u8c03\u7528\u8fd9\u4e2a\u65b9\u6cd5\uff0c\u7ed9\u5e94\u7528\u5e26\u6765\u7684\u6d88\u8017\u662f\u600e\u4e48\u6837\u7684\uff1f\u8fd9\u662f\u4e2a\u5feb\u901f\u7b80\u6d01\u7684\u65b9\u5f0f\u4e48\uff1f<\/p>\n\n\n\n<p>\u4e0a\u6d4b\u8bd5\u9a8c\u8bc1\u7684\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"java\" class=\"language-java\">\/**\n * Thread.currentThread().getStackTrace() \u83b7\u53d6\u4ee3\u7801\u7c7b\u540d\u4e0e\u884c\u53f7\u7684\u6d88\u8017\n *\/\npublic class CodeLine {\n\n    public static final boolean DEBUG = false;\n\n    public static void main(String[] args) {\n\n        CodeLine CL = new CodeLine();\n\n        CL.mainTestFunction(20,1000,1);\n        CL.mainTestFunction(20,1000,1);\n        CL.mainTestFunction(20,1000,1);\n        CL.mainTestFunction(20,1000,0);\n        CL.mainTestFunction(20,1000,0);\n        CL.mainTestFunction(20,1000,0);\n\n        CL.mainTestFunction(40,1000,1);\n        CL.mainTestFunction(40,1000,1);\n        CL.mainTestFunction(40,1000,1);\n        CL.mainTestFunction(40,1000,0);\n        CL.mainTestFunction(40,1000,0);\n        CL.mainTestFunction(40,1000,0);\n\n        CL.mainTestFunction(80,1000,1);\n        CL.mainTestFunction(80,1000,1);\n        CL.mainTestFunction(80,1000,1);\n        CL.mainTestFunction(80,1000,0);\n        CL.mainTestFunction(80,1000,0);\n        CL.mainTestFunction(80,1000,0);\n\n        CL.mainTestFunction(20,10,1);\n        CL.mainTestFunction(20,10,1);\n        CL.mainTestFunction(20,10,1);\n        CL.mainTestFunction(20,10,0);\n        CL.mainTestFunction(20,10,0);\n        CL.mainTestFunction(20,10,0);\n    }\n\n    \/**\n     *\n     * @param deep \u5806\u6808\u6df1\u5ea6\n     * @param count \u5faa\u73af\u6b21\u6570\n     * @param method \u83b7\u53d6\u65b9\u5f0f:0-\u5173\u95ed\uff1b1-Thread.currentThread().getStackTrace()\n     *\/\n    public void mainTestFunction(int deep,int count,int method)\n    {\n        long stt = System.nanoTime();\n        testStackTrace(deep,count,method);\n        long end = System.nanoTime();\n\n        System.out.println(String.format(\"\u7c7b\u578b:[%2d],\u6df1\u5ea6:[%10d],\u6b21\u6570:[%10d],\u8017\u65f6:[%10d]\u7eb3\u79d2\",method,deep,count,(end-stt)));\n    }\n\n\n    \/**\n     *\n     * @param deep \u5806\u6808\u6df1\u5ea6\n     * @param count \u5faa\u73af\u6b21\u6570\n     * @param method \u83b7\u53d6\u65b9\u5f0f:0-\u5173\u95ed\uff1b1-Thread.currentThread().getStackTrace()\n     *\/\n    public void testStackTrace(int deep,int count,int method)\n    {\n        if(deep!=0)\n        {\n            testStackTrace(deep-1,count,method);\n        }\n        else\n        {\n            for(int i=0;i&lt;count;i++)\n            {\n                testStackTraceDirect(i,method);\n            }\n        }\n    }\n\n    public void testStackTraceDirect(int currentCount,int method)\n    {\n        String className = \"\";\n        int lineNumber = 0 ;\n\n        switch (method){\n            case 1:\n               StackTraceElement[] result = Thread.currentThread().getStackTrace();\n               className = result[1].getClassName();\n               lineNumber = result[1].getLineNumber();\n        }\n\n        if(DEBUG&amp;&amp;currentCount==0)\n        {\n            System.out.println(\"className:\"+ className + \",line:\" + lineNumber);\n        }\n    }\n}\n\n<\/code><\/pre>\n\n\n\n<p>\u4e00\u6b21\u672c\u5730\u8fd0\u884c\u6548\u679c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"font-size:14px\"><code lang=\"bash\" class=\"language-bash\">\n\u7c7b\u578b:[ 1],\u6df1\u5ea6:[        20],\u6b21\u6570:[      1000],\u8017\u65f6:[  21663284]\u7eb3\u79d2\n\u7c7b\u578b:[ 1],\u6df1\u5ea6:[        20],\u6b21\u6570:[      1000],\u8017\u65f6:[  15444246]\u7eb3\u79d2\n\u7c7b\u578b:[ 1],\u6df1\u5ea6:[        20],\u6b21\u6570:[      1000],\u8017\u65f6:[  20645272]\u7eb3\u79d2\n\u7c7b\u578b:[ 0],\u6df1\u5ea6:[        20],\u6b21\u6570:[      1000],\u8017\u65f6:[     43295]\u7eb3\u79d2\n\u7c7b\u578b:[ 0],\u6df1\u5ea6:[        20],\u6b21\u6570:[      1000],\u8017\u65f6:[     47645]\u7eb3\u79d2\n\u7c7b\u578b:[ 0],\u6df1\u5ea6:[        20],\u6b21\u6570:[      1000],\u8017\u65f6:[     19836]\u7eb3\u79d2\n\u7c7b\u578b:[ 1],\u6df1\u5ea6:[        40],\u6b21\u6570:[      1000],\u8017\u65f6:[  30264831]\u7eb3\u79d2\n\u7c7b\u578b:[ 1],\u6df1\u5ea6:[        40],\u6b21\u6570:[      1000],\u8017\u65f6:[  28124662]\u7eb3\u79d2\n\u7c7b\u578b:[ 1],\u6df1\u5ea6:[        40],\u6b21\u6570:[      1000],\u8017\u65f6:[  25975522]\u7eb3\u79d2\n\u7c7b\u578b:[ 0],\u6df1\u5ea6:[        40],\u6b21\u6570:[      1000],\u8017\u65f6:[      6565]\u7eb3\u79d2\n\u7c7b\u578b:[ 0],\u6df1\u5ea6:[        40],\u6b21\u6570:[      1000],\u8017\u65f6:[      4495]\u7eb3\u79d2\n\u7c7b\u578b:[ 0],\u6df1\u5ea6:[        40],\u6b21\u6570:[      1000],\u8017\u65f6:[      5969]\u7eb3\u79d2\n\u7c7b\u578b:[ 1],\u6df1\u5ea6:[        80],\u6b21\u6570:[      1000],\u8017\u65f6:[  50870539]\u7eb3\u79d2\n\u7c7b\u578b:[ 1],\u6df1\u5ea6:[        80],\u6b21\u6570:[      1000],\u8017\u65f6:[  51951023]\u7eb3\u79d2\n\u7c7b\u578b:[ 1],\u6df1\u5ea6:[        80],\u6b21\u6570:[      1000],\u8017\u65f6:[  48961432]\u7eb3\u79d2\n\u7c7b\u578b:[ 0],\u6df1\u5ea6:[        80],\u6b21\u6570:[      1000],\u8017\u65f6:[      5279]\u7eb3\u79d2\n\u7c7b\u578b:[ 0],\u6df1\u5ea6:[        80],\u6b21\u6570:[      1000],\u8017\u65f6:[      4634]\u7eb3\u79d2\n\u7c7b\u578b:[ 0],\u6df1\u5ea6:[        80],\u6b21\u6570:[      1000],\u8017\u65f6:[      4502]\u7eb3\u79d2\n\u7c7b\u578b:[ 1],\u6df1\u5ea6:[        20],\u6b21\u6570:[        10],\u8017\u65f6:[    151152]\u7eb3\u79d2\n\u7c7b\u578b:[ 1],\u6df1\u5ea6:[        20],\u6b21\u6570:[        10],\u8017\u65f6:[    254739]\u7eb3\u79d2\n\u7c7b\u578b:[ 1],\u6df1\u5ea6:[        20],\u6b21\u6570:[        10],\u8017\u65f6:[    323122]\u7eb3\u79d2\n\u7c7b\u578b:[ 0],\u6df1\u5ea6:[        20],\u6b21\u6570:[        10],\u8017\u65f6:[       747]\u7eb3\u79d2\n\u7c7b\u578b:[ 0],\u6df1\u5ea6:[        20],\u6b21\u6570:[        10],\u8017\u65f6:[       481]\u7eb3\u79d2\n\u7c7b\u578b:[ 0],\u6df1\u5ea6:[        20],\u6b21\u6570:[        10],\u8017\u65f6:[       397]\u7eb3\u79d2\n<\/code><\/pre>\n\n\n\n<p>\u5176\u4e2d\u7c7b\u578b1\uff0c\u662f\u6267\u884c\u7c7b\u540d\u4e0e\u4ee3\u7801\u884c\u53f7\u83b7\u53d6\uff0c\u7c7b\u578b0\u662f\u6267\u884c\u8def\u5f84\u5b8c\u5168\u76f8\u540c\uff0c\u4f46\u662f\u4e0d\u8fdb\u884c\u7c7b\u540d\u4e0e\u884c\u53f7\u83b7\u53d6\u3002\u5bf9\u6bd4\u53d1\u73b0\uff0c\u5176\u6d88\u8017\u5dee\u522b\u662f\u975e\u5e38\u5927\u7684\u3002\u5806\u6808\u6df1\u5ea620\uff0c\u8c03\u752810\u6b21\u65e5\u5fd7\u6253\u5370\uff0c\u53ef\u80fd\u662f\u4e00\u4e2a\u5e94\u7528\u7cfb\u7edf\u4e2d\uff0c\u5f88\u5e38\u89c1\u7684\u4e00\u6b21\u4ea4\u6613\u5904\u7406\u5728\u67d0\u4e2a\b\u5e94\u7528\u4e2d\u7684\u573a\u666f\uff0c\u5728\u6253\u5370\u884c\u53f7\u7684\u60c5\u51b5\u4e0b\uff0c\u4ec5\u4ec5\u662f\u65e5\u5fd7\u8bed\u53e5\u7684\u6253\u5370\u884c\u53f7\u7684\u6d88\u8017\uff0c\u5c31\u6d88\u8017\u4e860.2~0.3\u6beb\u79d2\u3002\u8fd9\u8fd8\u4e0d\u662f\u4e00\u4e2a\u65e5\u5fd7\u5e93\u7684\u5168\u90e8\u6267\u884c\u65f6\u95f4\u3002<\/p>\n\n\n\n<p>\u5728\u6b63\u5f0f\u8fd0\u884c\u7684\u7cfb\u7edf\u4e2d\uff0c\u6b63\u5e38\u65e5\u5fd7\u6253\u5370\uff0c\u683c\u5f0f\u5b57\u7b26\u4e32\u4e2d\uff0c\u65e0\u9700\u8fdb\u884c\u4ee3\u7801\u7c7b\u4e0e\u884c\u53f7\u7684\u76f8\u5173\u914d\u7f6e\u3002\u5728\u51fa\u73b0\u5f02\u5e38\u65f6\uff0c\u5f02\u5e38\u5806\u6808\u81ea\u5e26\u76f8\u5173\u4fe1\u606f\u3002\u5728\u65e5\u5e38\u8c03\u8bd5\u65f6\uff0c\u53ef\u4ee5\u4fee\u6539\u683c\u5f0f\u5b57\u7b26\u4e32\uff0c\u6253\u5370\u76f8\u5173\u7684\u4fe1\u606f\uff0c\u4f46\u6b63\u5f0f\u8fd0\u884c\uff0c\u8bf7\u5173\u6389\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6211\u4eec\u5728\u4f7f\u7528Java\u7684\u65e5\u5fd7\u5e93\u8fdb\u884c\u65e5\u5fd7\u6253\u5370\u65f6\uff0c\u6709\u4e2a\u5fae\u5c0f\u7684\u529f\u80fd\uff0c\u6253\u5370\u5f53\u524d\u4ee3\u7801\u884c\u6240\u5728\u7684\u7c7b\u540d\uff0c\u65b9\u6cd5\u540d\u4ee5\u53ca\u884c\u53f7\u7b49\u4fe1\u606f\uff0c\u8fd9\u4e2a\u529f\u80fd\u662f\u5982\u4f55\u5b9e\u73b0\u7684\u5462\uff1f\u8bf4\u8d77\u6765\u8fd9\u8fd8\u662f\u4e00\u4e2a\u63a8\u52a8JDK\u53d1\u5c55\u7684\u4e8b\u60c5\uff0c\u4ece\u6700\u521d\u7684\u6ca1\u63d0\u4f9b\u76f8\u5173\u7684\u80fd\u529b\uff0c\u5230JDK1.5\u4e4b\u540e\u63d0\u4f9bThread.currentThread().getStackTrace()\u65b9\u6cd5\u80fd\u591f\u65b9\u4fbf\u7684\u83b7\u53d6StackTraceElement\u6570\u7ec4\uff0c\u8fdb\u800c\u65b9\u4fbf\u7684\u62ff\u5230\u7c7b\u540d\u4e0e\u884c\u53f7\u7b49\u4fe1\u606f\uff0c\u4e5f\u662f\u975e\u5e38\u597d\u7684\u4e00\u4e2a\u8fdb\u5316\u4f8b\u5b50\u3002\u4f46\u662f\u6211\u4eec\u8fd9\u91cc\u5173\u6ce8\u7684\u662f\u8c03\u7528\u8fd9\u4e2a\u65b9\u6cd5\uff0c\u7ed9\u5e94\u7528\u5e26\u6765\u7684\u6d88\u8017\u662f\u600e\u4e48\u6837\u7684\uff1f\u8fd9\u662f\u4e2a\u5feb\u901f\u7b80\u6d01\u7684\u65b9\u5f0f\u4e48\uff1f \u4e0a\u6d4b\u8bd5\u9a8c\u8bc1\u7684\u4ee3\u7801\u5982\u4e0b\uff1a \u4e00\u6b21\u672c\u5730\u8fd0\u884c\u6548\u679c\u5982\u4e0b\uff1a \u5176\u4e2d\u7c7b\u578b1\uff0c\u662f\u6267\u884c\u7c7b\u540d\u4e0e\u4ee3\u7801\u884c\u53f7\u83b7\u53d6\uff0c\u7c7b\u578b0\u662f\u6267\u884c\u8def\u5f84\u5b8c &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,23],"tags":[48,49,50,47],"class_list":["post-198","post","type-post","status-publish","format-standard","hentry","category-5","category-23","tag-getstacktrace","tag-stacktraceelement","tag-50","tag-47"],"_links":{"self":[{"href":"https:\/\/www.xudongvp.com\/index.php?rest_route=\/wp\/v2\/posts\/198","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xudongvp.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xudongvp.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xudongvp.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xudongvp.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=198"}],"version-history":[{"count":3,"href":"https:\/\/www.xudongvp.com\/index.php?rest_route=\/wp\/v2\/posts\/198\/revisions"}],"predecessor-version":[{"id":202,"href":"https:\/\/www.xudongvp.com\/index.php?rest_route=\/wp\/v2\/posts\/198\/revisions\/202"}],"wp:attachment":[{"href":"https:\/\/www.xudongvp.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=198"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xudongvp.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=198"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xudongvp.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=198"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}