标签: future

使用ExecutorService生成的结果.哪一堂课?

我希望我能写下类/接口名称,但我没有......

在查看JDK javadoc时,我看到了一个类/接口的引用,目的是收集和使用ExecutorService(完成的Futures<T>s)产生的结果,可能是系统中的其他地方.当时我记下了它,因为它非常适合我需要的东西,但我似乎无法从舌尖上得到这个类的名字.

任何人都知道我指的是什么?

java multithreading future executorservice

1
推荐指数
1
解决办法
172
查看次数

检查未来的价值

我想f通过查看其持有的价值来检查Future 是否现在完成.那不是

f onComplete {

}
Run Code Online (Sandbox Code Playgroud)

它是关于检查它当前没有阻塞的值.

你的意见?

scala future

1
推荐指数
1
解决办法
3204
查看次数

Scala - 按顺序依次执行任意数量的Futures

我正在试图找出按序列执行一系列期货的最佳方式,其中一个Future的执行取决于之前的.我试图为任意数量的期货做这件事.

用户案例:

  • 我从我的数据库中检索了许多ID.
  • 我现在需要检索Web服务上的一些相关数据.
  • 一旦我找到了有效的结果,我想停止.
  • 我只关心成功的结果.

并行执行这些并且然后解析返回的结果集合不是一种选择.我必须一次执行一个请求,并且只有在前一个请求没有返回任何结果时才执行下一个请求.

目前的解决方案是沿着这些方向.使用foldLeft执行请求,然后仅在前一个未来满足某些条件时评估下一个未来.

def dblFuture(i: Int) = { i * 2 }
val list = List(1,2,3,4,5)
val future = list.foldLeft(Future(0)) {
  (previousFuture, next) => {
    for {
      previousResult <- previousFuture
      nextFuture <- { if (previousResult <= 4) dblFuture(next) else previousFuture }
    } yield (nextFuture)
  }
}
Run Code Online (Sandbox Code Playgroud)

这方面的一个重大缺点是:a)我一直处理所有项目,即使我得到了一个我很满意的结果,并且b)一旦我找到了我想要的结果,我就会继续评估谓词.在这种情况下,它是一个简单的if,但实际上它可能更复杂.

我觉得我错过了一个更优雅的解决方案.

scala future sequential for-comprehension foldleft

1
推荐指数
1
解决办法
1644
查看次数

nvcc/CUDA 6.5&c ++ 11(未来) - gcc 4.4.7

当我编译包含设计C++ 11的以下代码时,我得到错误 - 它不编译.我试过不同的旗帜,但我还没有找到解决方案.

我的设置:CUDA 6.5,gcc 4.4.7我无法更改设置.我怎么还能做这个工作?

#include <stdio.h>
#include <vector>
#include "KD_tree.h"
#include "KD_tree.cpp"
#include <iostream>
#include <algorithm>
#include <cmath>
#include <future>
#define MYDEVICE 0

using namespace std;


int main()
{
    //do something..... 

    cudaDeviceProp devProp;
    cudaGetDeviceProperties(&devProp, MYDEVICE);
    printDevProp(devProp);
    int max_threads = devProp.warpSize;

   //do something else ... 

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我尝试使用不同的标志进行编译:

nvcc -std=c++11 cudaMain.cu KD_tree.cpp -arch=sm_20 -o tree.out
In file included from cudaMain.cu:14:
simple_kd_tree.h:12:19: warning: extra tokens at end of #include directive
cudaMain.cu:19:18: error: future: No such file or directory …
Run Code Online (Sandbox Code Playgroud)

gcc cuda future c++11

1
推荐指数
1
解决办法
473
查看次数

如何在scala中展平未来 - "val _2flat:Future [Option [Future [List [Long]]]]

我有这些数据:

val _2BeFlat: Future[Option[Future[List[Long]]]] = ...
Run Code Online (Sandbox Code Playgroud)

我需要:

val flat: Future[Option[List[Long]]] = ...
Run Code Online (Sandbox Code Playgroud)

有办法吗?

scala nested future

1
推荐指数
1
解决办法
1080
查看次数

Scala同时执行期货清单

我是斯卡拉的新手.它有助于减少代码并提供功能语言的元素来处理数据.但是,我无法找到并行执行lst期货的方法.我的列表是List [Future [String]]类型.如何让这个列表并行执行?

val futures=(data.map { x => this.breakString(x) }).toList
Run Code Online (Sandbox Code Playgroud)

未来定义为:

def breakString(inX:Object):Future[String]=Future {
    //get new jsonObject
    val x =inX.asInstanceOf[String]
    val jmap=JacksMapper.readValue[Map[String,AnyRef]](x)
    val dataArr:Array[String]=jmap.get(this.rowcolumn).asInstanceOf[String].split(token)
    val map=dataArr.map { x => (positions.get(dataArr.indexOf(x).asInstanceOf[String]),x) }.toMap
    map.put(hashKey, jmap.get(hashKey).asInstanceOf[String])

    //write out positions
    JacksMapper.writeValueAsString(map)
  }
Run Code Online (Sandbox Code Playgroud)

concurrency scala future

1
推荐指数
1
解决办法
1016
查看次数

如何将Future [Vector [UserLocation]]转换为Future [Vector [User]]

我有一个方法,我想返回Future[Vector[user]].

该方法userLocationService.getUserLocationsInList将返回一个Future[Vector[UserLocation]].

UserLocation看起来像这样:

case class UserLocation(id: Int, locationId: Int, userId: Int)


def getUsersInLocation(locationIdList: Set[Int]): Future[Vector[User]] = {

   userLocationService.getUserLocationsInList(locationIdList).map{
      userLocations =>
          // ????????????
   }

}
Run Code Online (Sandbox Code Playgroud)

我有一个方法,返回基于UserId的单个用户,如:

userService.getById(userId: Int): Future[User]
Run Code Online (Sandbox Code Playgroud)

如何根据上述内容构建Future [Vector [User]]?

scala future

1
推荐指数
1
解决办法
370
查看次数

期货让我有些困惑,尝试过flatMap身份

我在尝试编写返回Future [Map [Int,Long]]的方法时遇到问题.

我正在进行一些迭代,回到未来[未来[..]].所以我尝试了flatMap身份.

请参阅下面的我现在收到的代码和错误消息.我不知道此刻发生了什么.

def aggregate(permissions: Vector[Permission]): Map[Int, Long]

def calculate(roleProfile: RoleProfile): Future[Map[Int, Long]] = {
  val roleIds = roleProfile.roles.map(r => r.id)
  db.run(permissionDao.getByRoles(roleIds.toList)).map {
    permissions =>
      aggregate(permissions)
  }
}

def generate(roleGroupId: Int): Future[Map[Int, Long]] = {
  for {
    roleGroup <- roleGroupService.getById(roleGroupId)
    roles <- roleGroupService.getRolesByGroupId(roleGroupId)
  } yield {
    calculate(RoleProfile(roleGroup.get.id, roles.toSet)) //flatMap identity
  }
}
Run Code Online (Sandbox Code Playgroud)

我收到方法'计算'的错误消息:

type mismatch;
[error]  found   : scala.concurrent.Future[Map[Int,Long]]
[error]  required: Map[Int,Long]
[error]       calculate(RoleProfile(roleGroup.get.id, roles.toSet)) //flatMap identity
Run Code Online (Sandbox Code Playgroud)

现在,如果删除'flatMap identity'的注释,我会收到此错误:

type mismatch;
[error]  found   : Map[Int,Long] => Map[Int,Long]
[error] …
Run Code Online (Sandbox Code Playgroud)

scala future

1
推荐指数
1
解决办法
194
查看次数

如何在scala中使用外部未来的内在未来价值

嗨我有两个期货FutureA和FutureB我想从FutureB获取一个值并在FutureA中使用它但我得到的是null这里是代码

futureA = ask(ActorA, MessageA).mapTo[Int]
  FutureB = ask(ActorB, MessageB).mapTo[Int]
  var someResult=0

      futureA.map {
        result =>
             FutureB.map { x => 
                someResult=x//suppose it will retun 5
             }
             someResult=someResult+1//it should give 6
             println(someResult)//here i am getting 1
      }
Run Code Online (Sandbox Code Playgroud)

我想someResult返回,6但它返回1 我怎么能实现我不想使用阻塞调用await.result

scala future

1
推荐指数
1
解决办法
100
查看次数

无法读取通过hyper :: client :: Client发出HTTP请求的简单有效负载:特征绑定“ Body:Future”不满足

我正在尝试将转换ResultBuffer

let ufc_root: String =
    String::from("https://www.ufc.com/athletes/all?filters%5B0%5D=status%3A23");
// let ufc_root: String = String::from("https://www.google.com");
let https = HttpsConnector::new(4).unwrap();
let client = Client::builder().build::<_, hyper::Body>(https);

client
    .get(ufc_root.parse::<hyper::Uri>().unwrap())
    .and_then(|res| {
        println!("http status code: {}", res.status());
        println!("http response headers:\n{:?}: ", res.headers());
        res.into_body()
    })
    .from_err::<WebScrapeError>()
    .and_then(|body| {
        body.for_each(|chunk| {
            println!("{}", chunk.into_bytes());
        });

        let jon_jones = Subject {
            name: "Jon Jones".to_string(),
            link: "http://www.jonjones.com".to_string(),
        };
        let subjects = vec![jon_jones];
        Ok(subjects)
    })
    .from_err()
Run Code Online (Sandbox Code Playgroud)
let ufc_root: String =
    String::from("https://www.ufc.com/athletes/all?filters%5B0%5D=status%3A23");
// let ufc_root: String = String::from("https://www.google.com");
let https …
Run Code Online (Sandbox Code Playgroud)

future rust hyper

1
推荐指数
1
解决办法
208
查看次数