concurrency

concurrent access and free of a data structure

懵懂的女人 提交于 2019-12-23 20:53:23
问题 The problem is like this: I have an array of 500 pointers which point to 500 elements in a doubly linked list. There are 10 threads which run in parallel. Each thread runs 50 loops, and tries to free some element in the list. The list is sorted (contain simple integers), and there are 10 other threads running in parallel, searching for the node that contains a particular integer and access the other satellite data in this node. So the node is like: struct node { int key; // Key used to search

How to safely update a file that has many readers and one writer?

时间秒杀一切 提交于 2019-12-23 20:42:40
问题 I have a set of files. The set of files is read-only off a NTFS share, thus can have many readers. Each file is updated occasionally by one writer that has write access. How do I ensure that: If the write fails, that the previous file is still readable Readers cannot hold up the single writer I am using Java and my current solution is for the writer to write to a temporary file, then swap it out with the existing file using File.renameTo() . The problem is on NTFS, renameTo fails if target

Why does this program terminate on my system but not on playground?

风格不统一 提交于 2019-12-23 19:24:09
问题 Consider this program: package main import "fmt" import "time" import "runtime" func main() { x := 0 go func() { time.Sleep(500 * time.Millisecond) x = 1 }() for x == 0 { runtime.Gosched() } fmt.Println("it works!") } Why does it terminate locally but not on Playground? Does the termination of my program rely on undefined behavior? 回答1: That code doesn't offer much guarantees. It's relying almost entirely on implementation details around undefined behavior. In most multi-threaded systems,

Checking if periodic ScheduledFuture is running now

混江龙づ霸主 提交于 2019-12-23 19:18:12
问题 I have a periodic task scheduled via Spring TaskScheduler.schedule(Runnable, Trigger) . Given the returned ScheduledFuture , is there any way to check, if the task is running at current moment? 回答1: You can change you Runnable like this: class Runner implements Runnable{ public volatile boolean RUNNING = false; public void run(){ RUNNING = true; try{ // Your code } finally { RUNNING = false; } } } edit Thought operations with boolean are atomic and don't need to be volatile. 回答2: After a bit

Place a timeout on calls to an unresponsive Flask route (updated)

对着背影说爱祢 提交于 2019-12-23 19:14:02
问题 I currently have a route in a Flask app that pulls data from an external server and then pushes the results to the front end. The external server is occasionally slow or unresponsive. What's the best way to place a timeout on the route call, so that the front end doesn't hang if the external server is lagging? Or is there a more appropriate way to handle this situation in Flask (not Apache, nginx, etc)? My goal is to timeout a route call, not keep an arbitrary long process alive like this SO

How to deal with concurrency issues brought by NSStream run loop scheduling using GCD?

核能气质少年 提交于 2019-12-23 18:19:36
问题 I have the following situation where I create a GCD dispatch queue and in it I schedule an NSStream to the current NSRunLoop , as is required in its specification for it to emit delegate events, and then I make the run loop for that thread run using [[NSRunLoop currentRunLoop run] . This generates three possible scenarios: Create a serial queue in which an initial write message is sent through the stream and other write messages are only sent when there's a delegate callback from the NSStream

Concurrency issue while calling a native library via JNA

我是研究僧i 提交于 2019-12-23 17:33:56
问题 For an existing java application (which I do not have the source code) I am developing a plug-in which is calls a shared library. Unfortunately this shared library ( written in C ) is not thread safe. The application is calling my plugin within several concurrent threads hence the shared library is called by these concurrent threads and naturally it gives many errors due to concurrency ( e.g: already open files are prevented from being opened etc) I am accessing the shared library via JNA . I

Scala - futures does not run

感情迁移 提交于 2019-12-23 16:54:00
问题 I am trying to run the following future basic code future { println("ssss")} onSuccess{ case _ => println("succ")} However, when I run the main method, nothing to the console is printed and the system exits almost instantly. I am using the implicit ExecutionContext. Any hints? This code: val f = future(Await.ready(Promise().future, d.timeLeft)) f.onSuccess { case _ => println("hee") } also exits immediately.... 回答1: Futures are executed on a dedicated thread pool. If your main program does

Concurrent JUnit Tests with Parameters

白昼怎懂夜的黑 提交于 2019-12-23 16:46:10
问题 So I'm attempting to run parallel parameterized tests. I have a solution where the same test can run in parallel with the parameters supplied for example say I have the following: @Test public void someTest1(){ } @Test public void someTest2(){ } I can get someTest1() to run with all the parameters concurrently, but someTest2() will have still have to wait for someTest1() to complete with all parameters before executing. I was wondering if anyone knew of a solution to be able to run someTest1(

Best way to execute simple async task in Java?

我怕爱的太早我们不能终老 提交于 2019-12-23 16:45:56
问题 I want to asynchronously invoke a function which does something separately from main thread. I'm new in Java concurrency, so i ask what is the best way to perform action like this: for(File myFile : files){ MyFileService.resize(myfile) <--- this should be async } The while loop goes on while function MyFileService.resize works in background with each of my files in collection. I heard that CompletionStage from Java8 could be good way to do it. What is the best way? 回答1: How about Future in