concurrency

Program does not terminate immediately when all ExecutorService tasks are done

一世执手 提交于 2020-01-10 09:33:08
问题 I put a bunch of runnable objects into an ExecutorService: // simplified content of main method ExecutorService threadPool = Executors.newCachedThreadPool(); for(int i = 0; i < workerCount; i++) { threadPool.execute(new Worker()); } I would expect my program/process to stop immediately after all workers are done. But according to my log, it takes another 20-30 seconds until that happens. The workers do not allocate any resources, in fact, they do nothing at the moment. Don't get me wrong,

DbUpdateConcurrencyException using Entity Framework 6 with MySql

*爱你&永不变心* 提交于 2020-01-10 05:33:05
问题 I'm having trouble with concurrency checks using EF6 and MySQL. The problem I'm having is that I get a concurrency exception thrown when I try to save data to the database. If you examine the sql that is output to the console it tries to query the concurrency field from the database using the old value in the where clause. Because this field has been updated by the database. Environment: Windows 7 64 bit Visual Studio 2013 Nuget packages installed: EF 6.0.1 MySql.ConnectorNET.Data 6.8.3.2

Wrapping rate limiting API call

人走茶凉 提交于 2020-01-10 05:10:28
问题 I have access an API call that accepts a maximum rate of calls per second . If the rate is exceeded, an exception is thrown . I would like to wrap this call into an abstraction that does the necessary to keep the call rate under the limit. It would act like a network router: handling multiple calls and returning the results to the correct caller caring about the call rate. The goal is to make the calling code as unaware as possible about that limitation. Otherwise, every part in the code

Concurrent and Blocking Queue in Java

拥有回忆 提交于 2020-01-09 12:18:28
问题 I have the classic problem of a thread pushing events to the incoming queue of a second thread. Only this time, I am very interested about performance. What I want to achieve is: I want concurrent access to the queue, the producer pushing, the receiver poping. When the queue is empty, I want the consumer to block to the queue, waiting for the producer. My first idea was to use a LinkedBlockingQueue , but I soon realized that it is not concurrent and the performance suffered. On the other hand

Concurrent Priority Queue in .NET 4.0

孤者浪人 提交于 2020-01-09 09:58:09
问题 It seems there are lots of improvements in .NET 4.0 related to concurrency that might rely on concurrent priority queues. Is there decent priority queue implementation inside framework available for reuse? 回答1: There is an implementation as part of "Samples for Parallel Programming with the .NET Framework" at msdn. See ParallelExtensionsExtras. Direct link to source code for file ConcurrentPriorityQueue.cs 回答2: You may need to roll your own. A relatively easy way would be to have an array of

Goroutine does not execute if time.Sleep included

让人想犯罪 __ 提交于 2020-01-08 17:17:07
问题 The following code runs perfectly fine: package main import ( "fmt" ) func my_func(c chan int){ fmt.Println(<-c) } func main(){ c := make(chan int) go my_func(c) c<-3 } playgound_1 However if I change c<-3 to time.Sleep(time.Second) c<-3 playground_2 My code does not execute. My gut feeling is that somehow main returns before the my_func finishes executing, but it seems like adding a pause should not have any effect. I am totally lost on this simple example, what's going on here? 回答1: When

Maximum and minimum value of cobegin/coend block

烈酒焚心 提交于 2020-01-07 05:03:09
问题 Based on the following code, I need to find the minimum and maximum final values of x x=1 i=1 cobegin while (i<4) while (i<4) begin begin x=x*2 x=x*2 i=i+1 i=i+1 end end coend I figured that the minimum value x can have is 8 , if the the loops are executed in order. And the maximum value x can have is 16 , if the program enters one of the loops first, switches to the other loop and execute it until x=8 and i=4, and finishes the first loop, then x=16 and i=5. Is this correct? Am I missing any

Concurrency with reading but locking with mutating

坚强是说给别人听的谎言 提交于 2020-01-06 18:06:09
问题 I'm looking for a solution that allows multiple threads to read the shared resource (concurrency permitted) but then locks these reading threads once a thread enters a mutating block, to achieve best of both world. I've looked up this reference but it seems the solution is to lock both reading and writing threads. class Foo { List<string> sharedResource; public void reading() // multiple reading threads allowed, concurrency ok, lock this only if a thread enters the mutating block below. { }

Open card after certain time

北城余情 提交于 2020-01-06 17:55:21
问题 i'm programming a blackjack (single thread) for a university project and the dealer is the computer (e.g. no player action)... Does someone know how can I program in Java something like this: while (dealerpoints < 17) open card and repaint frame wait 1 sec (to run again the condition test for while) THe thing is, I don't want all dealer cards painted at once... Thanks in advance, Gabriel Sotero UPDATE: this is my code (that doesn't work) while (Dealer.getInstance().dealerPoints < 17){ Dealer

Implementation of singleton thread-safe list

北战南征 提交于 2020-01-06 15:16:00
问题 I'm using Spring framework. Need to have a list of objects, which should get all data from database at once. When data is changed, list will be null and next get operation should fill data from database again. Is my code correct for multi-thread environment? @Component @Scope("singleton") public class MyObjectHolder { private volatile List<MyObject> objectList = null; public List<MyObject> getObjectList() { if (objectList == null) { synchronized (objectList) { if (objectList == null) {