首页 \ 问答 \ 在Swift中有没有类似Java的“Future”?(Are there anything similar to Java's “Future” in Swift?)

在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,而不是整数

项目欧拉问题14(Collat​​z问题)

我建议的解决方案,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);
            }
        }
    }

}

相关问答

更多
  • 维基百科 : 在软件中,当调用堆栈使用太多内存时,会发生堆栈溢出。 在许多编程语言中,调用堆栈包含有限的内存量,通常在程序开始时确定。 该堆栈是一个数据结构,用于记录程序的子例程在完成执行时应将控制权返回给的点的记录。 当子程序被调用时,返回地址被压入栈中,当子程序结束执行时,返回地址被从栈中取出 。 如果有许多子程序并且堆栈中没有空间,则会发生堆栈溢出。 同样在堆栈中的目的是存储局部变量,所以如果局部变量太大,更可能堆栈没有空间来存储它,如果这种情况发生堆栈溢出。 当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 ...
  • 目前发现的大多数EXCEPTION_STACK_OVERFLOW错误都发生在JVM之外的本机代码中。 JVM 内部的崩溃值得一个错误报告,并且会被修复。 还是你需要一个(未知的)漏洞? 所以简单和最可靠的方法是编写一个本地库,其中有一些代码会导致JVM崩溃并使用JNI调用它。 (一般的答案,我实际上不知道如何做到这一点,不能用java代码完成;)) Most EXCEPTION_STACK_OVERFLOW errors I found so far happen in native code outsid ...
  • 为什么抛出堆栈溢出错误(一旦输入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 ...
  • 如果您只是想知道是否有办法设置堆栈大小,那么谷歌就是您的朋友。 处理是用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.
  • 你需要Longs,而不是整数 项目欧拉问题14(Collatz问题) 我建议的解决方案,DP public class Collatz { public static void main(String[] args) { List length = 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 ...
  • 你考虑过使用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 ...
  • 根据评论更新代码。 queueCollection ==> public class queueCollection implements java.io.Serializable { private String QueueName1; public String getQueueName1() { return QueueName1; } public void setQueueName1(String queueName1) { QueueName1 = queueName1; } ...

相关文章

更多

最新问答

更多
  • h2元素推动其他h2和div。(h2 element pushing other h2 and div down. two divs, two headers, and they're wrapped within a parent div)
  • 创建一个功能(Create a function)
  • 我投了份简历,是电脑编程方面的学徒,面试时说要培训三个月,前面
  • PDO语句不显示获取的结果(PDOstatement not displaying fetched results)
  • Qt冻结循环的原因?(Qt freezing cause of the loop?)
  • TableView重复youtube-api结果(TableView Repeating youtube-api result)
  • 如何使用自由职业者帐户登录我的php网站?(How can I login into my php website using freelancer account? [closed])
  • SQL Server 2014版本支持的最大数据库数(Maximum number of databases supported by SQL Server 2014 editions)
  • 我如何获得DynamicJasper 3.1.2(或更高版本)的Maven仓库?(How do I get the maven repository for DynamicJasper 3.1.2 (or higher)?)
  • 以编程方式创建UITableView(Creating a UITableView Programmatically)
  • 如何打破按钮上的生命周期循环(How to break do-while loop on button)
  • C#使用EF访问MVC上的部分类的自定义属性(C# access custom attributes of a partial class on MVC with EF)
  • 如何获得facebook app的publish_stream权限?(How to get publish_stream permissions for facebook app?)
  • 如何防止调用冗余函数的postgres视图(how to prevent postgres views calling redundant functions)
  • Sql Server在欧洲获取当前日期时间(Sql Server get current date time in Europe)
  • 设置kotlin扩展名(Setting a kotlin extension)
  • 如何并排放置两个元件?(How to position two elements side by side?)
  • 如何在vim中启用python3?(How to enable python3 in vim?)
  • 在MySQL和/或多列中使用多个表用于Rails应用程序(Using multiple tables in MySQL and/or multiple columns for a Rails application)
  • 如何隐藏谷歌地图上的登录按钮?(How to hide the Sign in button from Google maps?)
  • Mysql左连接旋转90°表(Mysql Left join rotate 90° table)
  • dedecms如何安装?
  • 在哪儿学计算机最好?
  • 学php哪个的书 最好,本人菜鸟
  • 触摸时不要突出显示表格视图行(Do not highlight table view row when touched)
  • 如何覆盖错误堆栈getter(How to override Error stack getter)
  • 带有ImageMagick和许多图像的GIF动画(GIF animation with ImageMagick and many images)
  • USSD INTERFACE - > java web应用程序通信(USSD INTERFACE -> java web app communication)
  • 电脑高中毕业学习去哪里培训
  • 正则表达式验证SMTP响应(Regex to validate SMTP Responses)