Scala:带有map的flatures,用于IO / CPU绑定任务的flatMap

时间:2017-06-27 14:40:27

标签: scala future

我知道在Java 8中,在流框架的过滤器,映射等方法中启动长时间运行的任务并不是一个好主意,因为无法调整底层的fork-join池它可能导致延迟问题和饥饿。

现在,我的问题是,Scala有什么问题吗?我试图谷歌它,但我想我不能把这个问题放在一个google-able句子里。

我们说我有一个对象列表,我想使用forEach将它们保存到数据库中,这会导致任何问题吗?我想这不会是Scala中的问题,因为功能转换是语言的基本构建块,但无论如何...

1 个答案:

答案 0 :(得分:2)

  • 如果您没有看到任何类型的I / O操作,那么使用期货可能是一种开销。

    def add(x: Int, y: Int) = Future { x + y }

  • 在Future构造函数中执行纯粹的CPU绑定操作会使逻辑执行速度变慢,而不是更快。对它们进行映射和平面映射可以增加这个问题的燃料。

  • 如果您想使用常量/简单计算初始化Future,可以使用Future.successful()

  • 但所有阻止I / O(包括SQL查询)都会被Future blocking包含在Future { DB.withConnection { implicit connection => val query = SQL("select * from bar") query() } }

    例如:

    import scala.concurrent.blocking Future { blocking { DB.withConnection { implicit connection => val query = SQL("select * from bar") query() } } 应该完成,

    blocking

  • scala.concurrent.ExecutionContext.global通知线程池此任务正在阻止。这允许池根据需要临时生成新工作程序。这样做是为了防止阻塞应用程序中的饥饿。

  • 线程池(默认为blocking池)知道images.foreach(i => { import scala.concurrent.blocking Future { blocking { DB.withConnection { implicit connection => val query = SQL("insert into .........") query() } } }) 中的代码何时完成。(因为它是一个fork连接线程池)

  • 因此,它将在完成后删除备用工作线程,并且池将缩小回预期的大小(默认为核心数)。

  • 但如果没有足够的内存来扩展线程池,这种情况也会适得其反。

  • 因此,对于您的方案,您可以使用

    #include<stdio.h>
    
    int main()
    {
        int n, t;
    
        printf("Enter the size of the array\n");
        scanf("%d", &n);
        int i, a[n];
    
        if ((n > 20) || (n <= 0))
            printf("Invalid Size");
        else
        {
            printf("Enter the values\n");
            for (i = 0; i < n; i++)
            {
                scanf("%d", &a[i]);
            }
    
            for (i = 0; i < n; i + 2)
            {
                if (a[i] > a[i + 2])
                {
                    t = a[i];
                    a[i] = a[i + 2];
                    a[i + 2] = t;
                }
            }
            for (i = 1; i < n; i + 2)
            {
                if (a[i] < a[i + 2])
                {
                    t = a[i];
                    a[i] = a[i + 2];
                    a[i + 2] = t;
                }
            }
            for (i = 0; i < n; i++)
            {
                printf("%d\n", a[i]);
            }
        }
    }
    
  • 如果您正在进行大量阻塞I / O,那么创建一个单独的线程池/执行上下文并执行该池中的所有阻塞调用是一个很好的做法。

参考文献:

希望这有帮助。

相关问题