What exactly does runtime.Gosched do?

Note: As of Go 1.5, GOMAXPROCS is set to the number of cores of the hardware: golang.org/doc/go1.5#runtime, below the original answer before 1.5. When you run Go program without specifying GOMAXPROCS environment variable, Go goroutines are scheduled for execution in single OS thread. However, to make program appear to be multithreaded (that’s what goroutines are … Read more

Will a chain of method calls (CompletableFuture API) execute asynchronously if the first method in a chain is asynchronous?

OK, so there are 3 types of methods in CompletableFuture. For example: thenApply() thenApplyAsync(Function) (without an Executor) thenApplyAsync(Function, Executor) (with an Executor) The last one means that this action will execute in the Executor that you pass to it and it is the most obvious one. The second one means that the action is executed … Read more

notifyAll() throws IllegalMonitorStateException

You’re calling wait and notify without synchronizing on the thing you’re waiting on or notifying. As documented in Object.notifyAll: Throws: IllegalMonitorStateException – if the current thread is not the owner of this object’s monitor. So this: synchronized(nameSetLock){ notifyAll(); } should be: synchronized(nameSetLock){ nameSetLock.notifyAll(); } … and ditto for wait. Note that your current code wouldn’t … Read more

EF Core takes a lot of time, sometimes, to perform SELECT query

It is not new question when SQL Server may slowdown queries because of Parameter Sniffing. Problem can be solved by converting parameters to constants or by adding OPTION(RECOMPILE) to the end of the query. This answer adds DbCommandInterceptor to DbContextOptions and appends OPTION(RECOMPILE) hint to particular queries. Configuring DbContext builder.UseSqlServer(connectionString) .UseRecompileExtensions(); // registering interceptor How … Read more

Futures / Success race

The use case is the first successful completion: scala> :pa // Entering paste mode (ctrl-D to finish) def firstSuccessOf[T](fs: Future[T]*)(implicit x: ExecutionContext): Future[T] = { val p = Promise[T]() val count = new java.util.concurrent.atomic.AtomicInteger(fs.size) def bad() = if (count.decrementAndGet == 0) { p tryComplete new Failure(new RuntimeException(“All bad”)) } val completeFirst: Try[T] => Unit = … Read more