我做了Anagram excersize,这就像Count Change问题,但有5万面额(硬币) . 我是not sure that it can be done iteratively,我不在乎 . 我只知道-xss选项没有任何效果 - 我总是在1024个堆栈帧之后失败(可能是scala在传递给java或printStackTrace限制时做错了 . 我不知道) . 无论如何,这是不好的选择 . 您不希望应用程序中的所有线程都是怪异的 . 但是,我用新的Thread(堆栈大小)做了一些实验 . 这确实有效,
def measureStackDepth(ss: Long): Long = {
var depth: Long = 0
val thread: Thread = new Thread(null, new Runnable() {
override def run() {
try {
def sum(n: Long): Long = {depth += 1; if (n== 0) 0 else sum(n-1) + 1}
println("fact = " + sum(ss * 10))
} catch {
case e: StackOverflowError => // eat the exception, that is expected
}
}
}, "deep stack for money exchange", ss)
thread.start()
thread.join()
depth
} //> measureStackDepth: (ss: Long)Long
for (ss measureStackDepth((scala.math.pow (10, ss)).toLong) )
//> fact = 10
//| (ss = 10^0 allows stack of size ,11)
//| fact = 100
//| (ss = 10^1 allows stack of size ,101)
//| fact = 1000
//| (ss = 10^2 allows stack of size ,1001)
//| fact = 10000
//| (ss = 10^3 allows stack of size ,10001)
//| (ss = 10^4 allows stack of size ,1336)
//| (ss = 10^5 allows stack of size ,5456)
//| (ss = 10^6 allows stack of size ,62736)
//| (ss = 10^7 allows stack of size ,623876)
//| (ss = 10^8 allows stack of size ,6247732)
//| (ss = 10^9 allows stack of size ,62498160)
您可以看到堆栈可以指数级地增长,并且指向更多的堆栈分配给线程 .