⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch04s03.html

📁 优秀的java程序开发软件方式与方法,对大家开发程序的时候有指导性帮助
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<html><head>      <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">   <title>3.&nbsp;&#22312;Java&#19978;&#23454;&#29616;Continuation&#65306;&#22522;&#20110;heap&#27861;</title><link rel="stylesheet" href="html.css" type="text/css"><meta name="generator" content="DocBook XSL Stylesheets V1.69.1"><link rel="start" href="index.html" title="Java&#32593;&#32476;&#31243;&#24207;&#21592;&#30475;Continuation"><link rel="up" href="ch04.html" title="Chapter&nbsp;4.&nbsp;Continuation&#30340;&#23454;&#29616;"><link rel="prev" href="ch04s02.html" title="2.&nbsp;&#22312;Java&#19978;&#23454;&#29616;Continuation&#65306;&#22522;&#20110;stack&#27861;"><link rel="next" href="ch05.html" title="Chapter&nbsp;5.&nbsp;RIFE&#31616;&#20171;"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">3.&nbsp;&#22312;Java&#19978;&#23454;&#29616;Continuation&#65306;&#22522;&#20110;heap&#27861;</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ch04s02.html">Prev</a>&nbsp;</td><th width="60%" align="center">Chapter&nbsp;4.&nbsp;Continuation&#30340;&#23454;&#29616;</th><td width="20%" align="right">&nbsp;<a accesskey="n" href="ch05.html">Next</a></td></tr></table><hr></div><div class="section" lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="d0e591"></a>3.&nbsp;&#22312;Java&#19978;&#23454;&#29616;Continuation&#65306;&#22522;&#20110;heap&#27861;</h2></div></div></div><p>&#25105;&#20204;&#21018;&#25165;&#35762;&#21040;&#29992;heap&#30340;&#26041;&#27861;&#20063;&#26159;&#21487;&#33021;&#30340;&#65292;&#25152;&#20197;&#25509;&#19979;&#26469;&#25105;&#20204;&#23601;&#20171;&#32461;&#19968;&#20010;&#36825;&#26679;&#30340;&#20363;&#23376;&#12290;&#24744;&#30340;&#31532;&#19968;&#20010;&#38382;&#39064;&#26159;&#65292;&#36825;&#21487;&#33021;&#21527;&#65311;&#25105;&#20204;&#24590;&#20040;&#21487;&#33021;&#35753;JVM&#25226;stack&#25918;&#21040;heap&#20013;&#21435;&#21602;&#65311;&#32473;&#24744;&#19968;&#20010;&#26263;&#31034;&#65306;CPS&#12290;&#35760;&#24471;&#25105;&#20204;&#20043;&#21069;&#35762;&#36807;&#29992;CPS&#26102;&#38656;&#35201;&#33258;&#24049;&#23454;&#29616;stack&#21527;&#65311;&#22240;&#20026;CPS&#20013;&#20989;&#25968;&#27704;&#36828;&#19981;&#20250;&#36820;&#22238;&#65292;&#25152;&#20197;JVM&#32500;&#25345;&#30340;stack&#20107;&#23454;&#19978;&#23436;&#20840;&#27809;&#26377;&#29992;&#22788;&#12290;&#20107;&#23454;&#19978;&#65292;&#22914;&#26524;runtime&#36275;&#22815;&#32874;&#26126;&#30340;&#35805;&#65292;&#23427;&#23601;&#33021;&#21457;&#29616;&#23427;&#20854;&#23454;&#19981;&#29992;&#32500;&#25345;stack&#65292;&#36825;&#20010;&#20248;&#21270;&#21483;&#20570;tail-call&#20248;&#21270;&#12290;&#19968;&#33324;&#24773;&#20917;&#19979;&#35201;&#25105;&#20204;&#19981;&#29992;&#35821;&#35328;&#25552;&#20379;&#30340;stack&#32780;&#26159;&#33258;&#24049;&#20889;&#26159;&#24456;&#35752;&#21388;&#30340;&#19968;&#20214;&#20107;&#65292;&#20294;&#26159;&#29616;&#22312;&#25105;&#20204;&#30495;&#26159;&#27714;&#20043;&#19981;&#24471;&#20102;&#65292;&#22240;&#20026;&#36825;&#26679;&#25105;&#20204;&#21487;&#20197;&#36731;&#26131;&#22320;&#25226;stack&#25918;&#21040;heap&#37324;&#12290;&#26469;&#30475;&#20363;&#23376;&#65306;</p><pre class="programlisting">public class Fib {    private static int fib(int n) {        if (n &lt;= 1) {            System.out.println("Fib(" + n + ") = " + n);            return n;        } else {            int f1 = fib(n - 1);            int f2 = fib(n - 2);            int f = f1 + f2;            System.out.println("Fib(" + n + ") = " + f);            return f;        }    }        public static void main(String[] args) {        fib(5);    }}</pre><p>&#36825;&#26102;&#19968;&#20010;&#24456;&#31616;&#21333;&#30340;&#31639;fibonacci&#25968;&#21015;&#30340;&#20363;&#23376;&#65292;&#20854;&#36755;&#20986;&#20026;&#65306;</p><pre class="screen">Fib(1) = 1Fib(0) = 0Fib(2) = 1Fib(1) = 1Fib(3) = 2Fib(1) = 1Fib(0) = 0Fib(2) = 1Fib(4) = 3Fib(1) = 1Fib(0) = 0Fib(2) = 1Fib(1) = 1Fib(3) = 2Fib(5) = 5</pre><p>&#29616;&#22312;&#25105;&#20204;&#25226;fib&#20989;&#25968;&#25913;&#20026;CPS&#27169;&#24335;&#12290;&#20551;&#35774;fib()&#65292;&#21152;&#27861;&#21644;System.out.print()&#37117;&#26159;CPS&#20989;&#25968;&#65292;&#25105;&#20204;&#21017;&#38656;&#35201;&#25226;fib()&#20989;&#25968;&#22312;&#35843;&#29992;&#19978;&#36848;&#20989;&#25968;&#22788;&#26029;&#24320;&#65292;&#24182;&#25226;&#20313;&#19979;&#30340;&#37096;&#20998;&#21464;&#25104;&#19968;&#20010;&#26032;&#30340;&#20989;&#25968;&#12290;&#25105;&#20204;&#21457;&#29616;&#25105;&#20204;&#38656;&#35201;&#22914;&#19979;&#20989;&#25968;&#65306;fib&#65292;retN&#65292;fibAddPrnRetF&#65292;addPrnRetF&#65292;prnRetF&#21644;retF&#12290;&#27599;&#20010;&#20989;&#25968;&#30340;&#21517;&#31216;&#26159;&#23427;&#34920;&#31034;&#30340;&#25805;&#20316;&#30340;&#32553;&#20889;&#65292;&#20363;&#22914;fibAddPrnRetF&#34920;&#31034;&#35745;&#31639;fib&#65292;&#25226;&#32467;&#26524;&#30456;&#21152;&#65292;&#36755;&#20986;&#65292;&#26368;&#21518;&#36820;&#22238;f&#12290;&#20107;&#23454;&#19978;&#65292;fibAddPrnRetF&#33258;&#24049;&#21482;&#20250;&#35843;&#29992;fib&#65292;&#20294;&#23427;&#20250;&#25226;addPrnRetF&#20316;&#20026;continuation&#20256;&#32473;fib&#65307;&#32780;addPrnRetF&#33258;&#24049;&#21482;&#20250;&#35843;&#29992;add&#65292;&#20294;&#23427;&#20250;&#25226;prnRetF&#20316;&#20026;continuation&#20256;&#32473;add&#65307;&#22914;&#27492;&#31561;&#31561;&#12290;</p><p>&#25972;&#20010;&#31243;&#24207;&#21464;&#25104;CPS&#22914;&#19979;&#65306;</p><pre class="programlisting">class Continuation {    Continuation nextOp;    StackFrame sf;    void go(int n) { go();     void go() { throw new RuntimeException(); }    Continuation(StackFrame sf) { this.sf = sf; }    Continuation prepend(Continuation cont) {        cont.nextOp = this;        return cont;    }}class StackFrame {    StackFrame nextFrame;    StackFrame prepend(StackFrame sf) {        sf.nextFrame = this;        return sf;    }}class FibStackFrame extends StackFrame {    int n, f1, f2, f;}class TerminateCont extends Continuation {    TerminateCont(StackFrame sf) {        super(sf);    }        void go(int n) {}    void go() {}}class FibCont extends Continuation {    FibCont(StackFrame sf) {        super(sf);    }        void go(int n) {        CpsFib.fib(n, sf, nextOp);    }}class RetNCont extends Continuation {    RetNCont(StackFrame sf) {        super(sf);    }        void go() {        CpsFib.retN(sf, nextOp);    }}class FibAddPrnRetFCont extends Continuation {    FibAddPrnRetFCont(StackFrame sf) {        super(sf);    }        void go(int n) {        CpsFib.fibAddPrnRetF(n, sf, nextOp);    }}class AddPrnRetFCont extends Continuation {    AddPrnRetFCont(StackFrame sf) {        super(sf);    }        void go(int n) {        CpsFib.addPrnRetF(n, sf, nextOp);    }}class PrnRetFCont extends Continuation {    PrnRetFCont(StackFrame sf) {        super(sf);    }        void go(int n) {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -