我尝试在scala中实现Producer Consumer程序而不使用Queue.因为我认为Actor已经实现了"邮件队列"或其他东西,所以再次编写代码将是多余的.
我试图纯粹在Actor中编写程序.以下是多生产者多个消费者计划.制片人睡了一会儿,模拟做某事.消费者根本不睡觉.
但是我不知道如果我没有添加监督者演员来监视消费者,如何关闭程序,以及使用"Await"(代码中的监督者类)的Promise对象
反正有没有摆脱他们?
import akka.actor.Actor.Receive
import akka.actor._
import akka.routing._;
import akka.util._
import scala.concurrent.{Await, Promise}
import scala.concurrent.duration._
class Producer(val pool:ActorRef)(val name:String) extends Actor {
def receive = {
case _ =>
while (true) {
val sleepTime = scala.util.Random.nextInt(1000)
Thread.sleep(sleepTime)
println("Producer %s send food" format name)
pool ! name
}
}
}
class Consumer(supervisor : ActorRef)(val name:String) extends Actor {
var counter = 0
def receive = {
case s =>
counter += 1
println("%s eat food produced by %s" …Run Code Online (Sandbox Code Playgroud) 在我问这个问题之前,我只在dev模式和其他几个帖子中阅读了Play框架java.lang.NoClassDefFoundError.他们没有帮助我解决我的问题.
我使用Typesafe Activator 1.2.12和play-scala模板创建了一个项目.我也使用了Slick 2.1.0.
我想嵌入带有h2gis扩展名的h2数据库.
使用Play项目时,浏览器中会显示activator run以下Execution exception内容:

控制台上有以下堆栈跟踪:
18013 [play-akka.actor.default-dispatcher-3] ERROR application -
! @6kmnab7c4 - Internal server error, for (GET) [/2] ->
@6kmnab7c4: Execution exception in null:null
at play.api.Application$class.handleError(Application.scala:296)
at play.api.DefaultApplication.handleError(Application.scala:402)
at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$14$$anonfun$apply$1.applyOrElse(PlayDefaultUpstreamHandler.scala:205)
at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$14$$anonfun$apply$1.applyOrElse(PlayDefaultUpstreamHandler.scala:202)
at scala.runtime.AbstractPartialFunction.apply(AbstractPartialFunction.scala:36)
at scala.util.Failure$$anonfun$recover$1.apply(Try.scala:215)
at scala.util.Try$.apply(Try.scala:191)
at scala.util.Failure.recover(Try.scala:215)
at scala.concurrent.Future$$anonfun$recover$1.apply(Future.scala:324)
at scala.concurrent.Future$$anonfun$recover$1.apply(Future.scala:324)
at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:32)
at play.api.libs.iteratee.Execution$trampoline$.execute(Execution.scala:46)
at scala.concurrent.impl.CallbackRunnable.executeWithValue(Promise.scala:40)
at scala.concurrent.impl.Promise$DefaultPromise.tryComplete(Promise.scala:248)
at scala.concurrent.Promise$class.complete(Promise.scala:55)
at scala.concurrent.impl.Promise$DefaultPromise.complete(Promise.scala:153)
at scala.concurrent.impl.Future$PromiseCompletingRunnable.run(Future.scala:23)
at akka.dispatch.TaskInvocation.run(AbstractDispatcher.scala:41)
at akka.dispatch.ForkJoinExecutorConfigurator$AkkaForkJoinTask.exec(AbstractDispatcher.scala:393)
at scala.concurrent.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260) …Run Code Online (Sandbox Code Playgroud) 从K20开始,不同的流变为完全并发(用于在边缘上并发).
但是我的程序需要旧的方式.或者我需要做很多同步来解决依赖问题.
是否可以将流管理切换为旧方式?
以下是两个模板函数,它们的模板参数不同.其余参数完全相同.
template<int module>
void template_const(int &a,int & b){
a = a & module;
b = b % module;
}
template<bool x>
void template_const(int &a,int & b){
int w;
if (x){
w = 123;
}
else w = 512;
a = a & w;
b = b % w;
}
Run Code Online (Sandbox Code Playgroud)
当我试图像这样打电话给他们
template_const<true>(a,b)
Run Code Online (Sandbox Code Playgroud)
要么
template_const<123>(a,b)
Run Code Online (Sandbox Code Playgroud)
编译器告诉我调用是不明确的.我怎么称呼这两个功能?
我想意识到
fun1 f a_ziplist
Run Code Online (Sandbox Code Playgroud)
例如
getZipList $ (\x y z -> x*y+z) <$> ZipList [4,7] <*> ZipList [6,9] <*> ZipList [5,10]
f = (\x y z -> x*y+z)
ziplist = [[4,7],[6,9],[5,10]]
Run Code Online (Sandbox Code Playgroud)
为此,我想递归地应用<*>
foldx (h:w) = h <*> foldx w
foldx (w:[]) = w
Run Code Online (Sandbox Code Playgroud)
但似乎不可能使<*>递归。
我想将一个函数fun1作为参数传递给fun2.但是fun1需要一个隐含参数.是否可以在里面定义隐含值fun2?
代码是这样的:
import org.json4s._
import org.json4s.jackson.JsonMethods._
def fun1(json:JValue)(implicit formats: Formats) = {
//do something
}
def fun2(f: (JValue) => RatEvent,line:String ) = {
implicit val formats = DefaultFormats //has been defined in import
val json = parse(line) //covert string to jvalue
val result = f(json)
}
Run Code Online (Sandbox Code Playgroud)
在这里我传递fun1给fun2,编译器抱怨它找不到fun1的隐含值.
fun2(fun1,"asdfasdf") //error here, fun1 is lack of an implicit value
Run Code Online (Sandbox Code Playgroud)
我想通过改变fun2ie 的形状来解决问题
def fun2(f: (JValue)(implicit Formats) => RatEvent,line:String ) …Run Code Online (Sandbox Code Playgroud) Spark提供的方法saveAsTextFile可以RDD[T]轻松存储到磁盘或hdfs中.
T是任意可序列化的类.
我想扭转操作.我想知道是否有一个loadFromTextFile可以轻松加载文件RDD[T]?
让我说清楚:
class A extends Serializable {
...
}
val path:String = "hdfs..."
val d1:RDD[A] = create_A
d1.saveAsTextFile(path)
val d2:RDD[A] = a_load_function(path) // this is the function I want
//d2 should be the same as d1
Run Code Online (Sandbox Code Playgroud) 我有一个trait A方法,def fun1( b:B ):C
我想要A的子类实现 B 具有更详细的类型:
这是代码:
trait B
trait C
trait A {
def fun1( b:B ):C
}
class B1 extends B{
}
class B2 extends B{
}
class C1 extends C{
}
class C2 extends C{
}
Run Code Online (Sandbox Code Playgroud)
我希望 的A子类可以声明如下
class X1 extends A{
override def fun1(b:B1):C1 = ...
}
class X2 extends A{
override def fun1(b:B2):C2 = ...
}
Run Code Online (Sandbox Code Playgroud)
然而,编译器会抱怨X1 overrides nothing. 我必须手动匹配DetailedB的类型,如下所示。
class X1 …Run Code Online (Sandbox Code Playgroud) 我需要将代码写入几个.cu文件中。但是我应该在哪里定义许多.cu文件使用的设备变量。
一个例子
文件common.h
__device__ int x;
Run Code Online (Sandbox Code Playgroud)
文件A.cu
__global__ void a()
Run Code Online (Sandbox Code Playgroud)
文件B.cu
__global__ void b()
Run Code Online (Sandbox Code Playgroud)
a(),b()都使用x。我该怎么办?
在C语言中,我应该编写类似extern device int x的代码;然后在另一个地方定义设备 int x。但是在CUDA中我做不到。如果我这样做,它会告诉我先前在此处声明的“ ..........”
我已阅读TypeTag相关文章,但我无法实现按元素类型过滤集合.
例:
trait A
class B extends A
class C extends A
val v = Vector(new B,new C)
v filter ( _.isInstanceOf[B] )
Run Code Online (Sandbox Code Playgroud)
上面的代码工作正常.但是我想要提取filter出来的v.例如
def filter[T,T2](data:Traversable[T2]) = (data filter ( _.isInstanceOf[T])).asInstanceOf[Traversable[T]]
//Then filter v by
filter[B,A](v)
Run Code Online (Sandbox Code Playgroud)
在这种情况下,我得到警告abstract type T is unchecked since it is eliminated by erasure.我尝试使用TypeTag,但似乎不容易Type上运行.
实现功能有没有优雅的解决方案filter?通过scala宏的任何解决方案也是可以接受的.
scala ×5
cuda ×2
actor ×1
akka ×1
apache-spark ×1
c ×1
c++ ×1
concurrency ×1
cuda-streams ×1
generics ×1
h2 ×1
haskell ×1
header-files ×1
jts ×1
overloading ×1
overriding ×1
rdd ×1
runtime-type ×1
slick ×1
templates ×1