我试图测试ExecutionContext在游戏应用程序的行为,发现我不能够实现并行的任何程度时,我使用的是默认调度程序通过调用as.dispatcher,as.dispatchers.lookup("akka.actor.default-dispatcher")或通过将默认执行上下文作为参数传递给我的控制器类:
class HomeController @Inject()(cc: ControllerComponents)(implicit ec: ExecutionContext)
Run Code Online (Sandbox Code Playgroud)
我以此处提供的播放示例为基础。并添加/更改以下配置:
路线
GET /futures controllers.HomeController.testFutures(dispatcherId: String)
Run Code Online (Sandbox Code Playgroud)
common.conf
akka {
my-dispatcher {
executor = "fork-join-executor"
fork-join-executor {
# vm-cores = 4
parallelism-min = 4
parallelism-factor = 2.0
# 2x vm-cores
parallelism-max = 8
}
}
actor.default-dispatcher {
executor = "fork-join-executor"
fork-join-executor {
# vm-cores = 4
parallelism-min = 4
parallelism-factor = 2.0
# 2x vm-cores
parallelism-max = 8
}
}
}
Run Code Online (Sandbox Code Playgroud)
家庭控制器
@Singleton
class HomeController @Inject()(cc: …Run Code Online (Sandbox Code Playgroud) 我有一个关于使用fork join线程池的简单问题.这是我正在使用的一个简短示例:
executor = "fork-join-executor"
# Configuration for the fork join pool
fork-join-executor {
# Min number of threads to cap factor-based parallelism number to
parallelism-min = 24
# Parallelism (threads) ... ceil(available processors * factor)
parallelism-factor = 4.0
# Max number of threads to cap factor-based parallelism number to
parallelism-max = 48
}
Run Code Online (Sandbox Code Playgroud)
我不确定的是,在这种情况下会创建多少个线程?我正在运行2核心机器,因此每个核心有24个线程,最多48个线程?
并行因子设置为4.0时,可并行运行的线程数将为8.那么设置最小值和最大值的需要是什么,我的情况是24和48?
考虑以下代码:
private static async Task Main(string[] args)
{
await SetValueInAsyncMethod();
PrintValue();
await SetValueInNonAsyncMethod();
PrintValue();
}
private static readonly AsyncLocal<int> asyncLocal = new AsyncLocal<int>();
private static void PrintValue([CallerMemberName] string callingMemberName = "")
{
Console.WriteLine($"{callingMemberName}: {asyncLocal.Value}");
}
private static async Task SetValueInAsyncMethod()
{
asyncLocal.Value = 1;
PrintValue();
await Task.CompletedTask;
}
private static Task SetValueInNonAsyncMethod()
{
asyncLocal.Value = 2;
PrintValue();
return Task.CompletedTask;
}
Run Code Online (Sandbox Code Playgroud)
如果在.NET 4.7.2控制台应用程序中运行此代码,则将获得以下输出:
SetValueInAsyncMethod: 1
Main: 0
SetValueInNonAsyncMethod: 2
Main: 2
Run Code Online (Sandbox Code Playgroud)
我确实知道,输出的差异是由以下事实引起的:该事实实际上SetValueInAsyncMethod不是方法,而是由状态机执行的,AsyncTaskMethodBuilder该状态机在ExecutionContext内部捕获并且SetValueInNonAsyncMethod只是常规方法。
但是即使有了这种理解,我仍然有一些问题: …
当一个线程因异常而死亡时,该线程会发生什么?如果它在线程池中,它是否会产生一个新线程?我对scala ExecutionContext中发生的事情感兴趣,但由于ExecutionContext包装了一个java线程池,我认为Java用户也会知道答案.
例如,如果我创建一个包装FixedThreadPool(100)的ExecutionContext,如果一个线程死掉,那么线程池是否会替换该线程?
关于总结问题的标题 - 这是代码示例:
!function() {
console.log(this); // global object
}();
(function() {
console.log(this); // global object
})();
() => {
console.log(this); // {}
}();
var x = (function() {
console.log(this); // global object
})();
Run Code Online (Sandbox Code Playgroud)
箭头功能背后发生了什么?如果我想在ES5中使用该范围,据我所知,我必须将执行绑定到一个空对象,如下所示:
!function() {
console.log(this); // global object
}.bind({})();
Run Code Online (Sandbox Code Playgroud) 有一些关于JS中的对象,执行上下文,我不明白.
当我们创建一个对象时,它是否创建了一个执行上下文?因为在调用函数时会创建执行上下文.如果没有,那么对象就像实际执行上下文中的其他变量一样?
谢谢.
像这样的方法传递更惯用的Scala吗ExecutionContext
class Foo {
def bar(a: Int, b: Int)(implicit ec: ExecutionContext): Future[Int] = {
Future(a + b)
}
def baz(a: Int, b: Int)(implicit ec: ExecutionContext): Future[Int] = {
Future(a - b)
}
}
Run Code Online (Sandbox Code Playgroud)
或者更好地通过ExecutionContext每个班级
class Foo(implicit ec: ExecutionContext) {
def bar(a: Int, b: Int): Future[Int] = {
Future(a + b)
}
def baz(a: Int, b: Int): Future[Int] = {
Future(a - b)
}
}
Run Code Online (Sandbox Code Playgroud)
是一种样式在Scala世界中通常更受欢迎,是因为它引起较少的惊喜,更易于阅读或其他原因?如果可能,请提供一些参考。
到处都可以看到,在执行长时间运行的操作或阻塞操作时,最好使用特殊的Execution上下文。阻止操作,例如访问数据库。我明白为什么。这是为了避免线程饥饿。我们不想让“ 8”个可用线程忙于一些可能最终返回或保持阻塞的阻塞代码。它会严重降低应用程序速度或无限期阻止它。
同时,我想知道如何实现Spray或Play之类的东西。确实,让我们来看看客户端。发送请求后,我们将获得以后的答复。换句话说,请求是异步执行的。顺便说一句,这可能会导致长时间运行。但是,没有什么可以说启动许多请求会导致线程匮乏。因此,我想知道为什么在这种情况下这不是问题。他们有特殊的线程池吗?
我在《学习Scala中的并发编程》一书中提到,在Future中使用“ Blocking {}”语句块可以帮助其调度程序自动产生更多线程。难道是他们处理的方式?
接收请求可以说是一样的,在游戏中我们可以执行一个异步动作。如果要通过此操作访问数据库,则应使用“ Blocking {}”语句块。如何执行该操作是一个特殊的threadPool / ExecutionContext。
我在这里的假设是,它们依赖于hidden.global ExecutionContext。也许我错了。底线是。默认情况下,发出请求是一个漫长的操作,例如在代码中使用spray会如何处理它,从而不会在代码中创建线程饥饿?
我们使用不同的ExecutionContext吗?
编辑:刚刚发现了这个简短的演示文稿,请不要阻塞-如何弄清Akka和Spray的情况,以更好地说明我在这里遇到的问题。
无论如何,我希望能有其他意见
编辑:这是我了解到的使用未来时会发生的事情:
def apply[T](body: =>T): Future[T] = impl.Future(body) //here I have omitted the implicit ExecutorContext
impl.Future is an implementation of Future trait:
def apply[T](body: =>T)(implicit executor: ExecutionContext): scala.concurrent.Future[T] =
{
val runnable = new PromiseCompletingRunnable(body)
executor.prepare.execute(runnable)
runnable.promise.future
}
Run Code Online (Sandbox Code Playgroud)
PromiseCompletingRunnable如下所示:
class PromiseCompletingRunnable[T](body: => T) extends Runnable {
val promise = new Promise.DefaultPromise[T]()
override def run() = {
promise complete {
try Success(body) catch { …Run Code Online (Sandbox Code Playgroud) 从MDN:https : //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this,它说:
但是,在严格模式下,此值将保持为进入执行上下文时设置的值,因此,在以下情况下,它将默认为未定义:
function f2() {
'use strict'; // see strict mode
return this;
}
f2() === undefined; // true
Run Code Online (Sandbox Code Playgroud)
这表明如果我(1)“使用严格”;(2)在另一个函数中定义f2,调用f2将为f2绑定外部函数的this。但!
没用...
function f2() {
'use strict'; // see strict mode
return this;
}
f2() === undefined; // true
Run Code Online (Sandbox Code Playgroud)
给出以下输出:
hello, let's see some weird stuff
THIS in global {}
outer AlarmClock { clockName: 'Horizons' }
inner undefined
withoutOut { Moose: 'genius' }
modifiedTheThis, should have Howard { Moose: 'genius', howard: 'notSoBad' }
withoutIn1 undefined
Strict should …Run Code Online (Sandbox Code Playgroud) 我有这种情况:
方法a:创建一个隐式ec
方法a:调用Future中的另一个方法,即Future(anotherMethod)。anotherMethod,并且其所有后续调用都不再具有方法 a 中的 ec 范围。
示例代码:
class Foo {
private implicit val ec: ExecutionContextExecutor =
ExecutionContext.fromExecutor(Executors.newFixedThreadPool(Runtime.getRuntime.availableProcessors()))
private val anotherClass = new Bar()
def methodA() = Future(anotherClass.anotherMethod())
}
Run Code Online (Sandbox Code Playgroud)
我猜测,任何对.par,例如someVector.par.map.().seqetc 的调用anotherMethod或其任何后续调用都将使用全局执行上下文,而不是在方法 a 中创建的自定义上下文。我的假设正确吗?
我试图重写这百万种方法,无法弄清楚Tony Alicea如何从这段代码中产生结果1,2,undefined,1:
function b() {
var myVar;
console.log(myVar);
}
function a() {
myVar = 2;
console.log(myVar)
b();
}
var myVar = 1;
console.log(myVar);
a();
console.log(myVar);
Run Code Online (Sandbox Code Playgroud)
您可以在https://www.youtube.com/watch?v=Bv_5Zv5c-Ts&t=74m30s上查看代码和视频,执行此操作并生成1,2,未定义,1.我继续运行并获得1, 2,undefined,2.我正在做的是导致myVar在全局范围和()范围内以2存在吗?我的代码目前发布在https://testing-mdmitchellnyc.c9.io/hello-world.html.
executioncontext ×11
scala ×5
javascript ×4
akka ×3
future ×2
.net ×1
async-await ×1
c# ×1
concurrency ×1
ecmascript-6 ×1
fork-join ×1
function ×1
iife ×1
java ×1
object ×1
scope ×1
spray ×1
this ×1
threadpool ×1