在Swift中有没有类似Java的“Future”?(Are there anything similar to Java's “Future” in Swift?)
Java有Future或FutureTask,可以在新线程中运行任务。 然后,将执行结果返回到原始线程。 Swift中有没有什么功能可以实现这一点?
Java has Future or FutureTask that can run a task in a new thread. Then, return the execution result to the original thread. Are there any feature in Swift can achieve that?
原文:https://stackoverflow.com/questions/26539019
更新时间:2023-06-03 15:06
最满意答案
你需要Longs,而不是整数
我建议的解决方案,DP
public class Collatz { public static void main(String[] args) { List<Long> length = new ArrayList<Long>(); Map<Long,Long> dict = new HashMap<Long,Long>(); for(int i = 13; i < 1000000; i++){ length.add(collat(i, 0,dict)); } } public static long collat(long x, long c, Map<Long,Long> dict){ if(dict.containsKey(x)) { return dict.get(x); } if(x == 1){ dict.put(x, c); return c; } else { if(x % 2 == 0){ dict.put(x, collat(x/2, c + 1,dict)); return dict.get(x); }else{ dict.put(x,collat((3 * x) + 1, c + 1,dict)); return dict.get(x); } } } }
You need Longs, not Integers
Project Euler Question 14 (Collatz Problem)
My suggested solution, with DP
public class Collatz { public static void main(String[] args) { List<Long> length = new ArrayList<Long>(); Map<Long,Long> dict = new HashMap<Long,Long>(); for(int i = 13; i < 1000000; i++){ length.add(collat(i, 0,dict)); } } public static long collat(long x, long c, Map<Long,Long> dict){ if(dict.containsKey(x)) { return dict.get(x); } if(x == 1){ dict.put(x, c); return c; } else { if(x % 2 == 0){ dict.put(x, collat(x/2, c + 1,dict)); return dict.get(x); }else{ dict.put(x,collat((3 * x) + 1, c + 1,dict)); return dict.get(x); } } } }
相关问答
更多-
什么是堆栈溢出?(What is a stack overflow?)[2023-11-24]
维基百科 : 在软件中,当调用堆栈使用太多内存时,会发生堆栈溢出。 在许多编程语言中,调用堆栈包含有限的内存量,通常在程序开始时确定。 该堆栈是一个数据结构,用于记录程序的子例程在完成执行时应将控制权返回给的点的记录。 当子程序被调用时,返回地址被压入栈中,当子程序结束执行时,返回地址被从栈中取出 。 如果有许多子程序并且堆栈中没有空间,则会发生堆栈溢出。 同样在堆栈中的目的是存储局部变量,所以如果局部变量太大,更可能堆栈没有空间来存储它,如果这种情况发生堆栈溢出。 当DrawLine子程序从另一个名为Dr ... -
改变int djathtas = size - 1; to int djathtas = size; 并change quicksort(majtas, djathtas, p); quicksort(majtas, djathtas - 1, p); 否则它不会分配10位数,只有9位数。 看起来你只是使用了太大的数字。 您的程序可以正常运行并生成堆栈溢出错误。 您可以尝试实现不依赖于堆栈或其他东西的另一个版本的快速排序。 除此之外,我不知道为什么你还需要这么大的输入。 Change int djathta ...
-
如何在Java中重现EXCEPTION_STACK_OVERFLOW错误(How can I reproduce EXCEPTION_STACK_OVERFLOW error in Java)[2023-01-28]
目前发现的大多数EXCEPTION_STACK_OVERFLOW错误都发生在JVM之外的本机代码中。 JVM 内部的崩溃值得一个错误报告,并且会被修复。 还是你需要一个(未知的)漏洞? 所以简单和最可靠的方法是编写一个本地库,其中有一些代码会导致JVM崩溃并使用JNI调用它。 (一般的答案,我实际上不知道如何做到这一点,不能用java代码完成;)) Most EXCEPTION_STACK_OVERFLOW errors I found so far happen in native code outsid ... -
Stack Overflow错误:将struct传递给函数vc ++(Stack Overflow error : Passing struct to function vc++)[2022-02-07]
为什么抛出堆栈溢出错误(一旦输入ReadFiles()? 那是因为FILE_DATA [3]为堆栈内存分配了太多字节。 堆栈内存的大小默认大约为1Mb,而FILE_DATA [3]的大小大约为2.4Mb(~800,000 x 3字节)。 如果使用大型结构,请尝试使用堆内存,如下所示: void ReadFiles() { FILE_DATA* Files = new FILE_DATA[3]; bReadFileData("C:\\Test1.txt", &Files[0]); bReadFil ... -
处理堆栈溢出(stack overflow under processing)[2022-04-09]
如果您只是想知道是否有办法设置堆栈大小,那么谷歌就是您的朋友。 处理是用Java编写的,因此google搜索“java set stack size”这样的东西将是一个很好的起点。 实际上,StackOverflow多次询问了这个问题: 如何增加Java堆栈大小? Java堆栈溢出错误 - 如何在Eclipse中增加堆栈大小? Java Applet:增加堆栈大小 java设置最大堆栈大小 Java默认堆栈大小 但是,由于您正在使用Processing,因此您必须将其导出为可运行的jar,或者从eclips ... -
C ++不会像托管环境那样持有你的手。 发生堆栈溢出意味着未定义的行为。 C++ won't hold your hand as a managed enviroment does. Having a stack overflow means undefined behaviour.
-
任何人都可以向我解释为什么我会收到Stack Overflow Error?(Can anybody explain to me why I am getting Stack Overflow Error?)[2021-10-09]
你需要Longs,而不是整数 项目欧拉问题14(Collatz问题) 我建议的解决方案,DP public class Collatz { public static void main(String[] args) { Listlength = new ArrayList (); Map dict = new HashMap (); for(int i = 13; i < 1000000; i++){ ... -
“自我调用方法”更正确地称为“递归方法” 。 你的解决方案很有创意,我会给你。 但不要这样做。 堆栈空间非常有限。 当你转向服务时,你会看到这个问题,并且有更好的方法来处理这个问题。 计时器在服务中使用时非常合适。 A "self-calling method" is more correctly called a "recursive method". Your solution is creative, I'll give you that. But don't do it. Stack space i ...
-
调用mergeSort方法时出现Stack_Overflow_error(Stack_Overflow_error while calling mergeSort method)[2023-01-06]
你考虑过使用JDK中的Collections.sort(),它会进行合并排序吗? Java 8还有一个parallelSort,它执行并行合并排序。 I’d like to give you a update. By changing the stack size, I did able to run my program properly. The reason it was causing Exception in thread "main" java.lang.StackOverflowError i ... -
堆栈溢出错误(Stack overflow error)[2021-09-14]
根据评论更新代码。 queueCollection ==> public class queueCollection implements java.io.Serializable { private String QueueName1; public String getQueueName1() { return QueueName1; } public void setQueueName1(String queueName1) { QueueName1 = queueName1; } ...