Questions tagged [concurrency]

In computer science, concurrency is a property of systems in which multiple computations can be performed in overlapping time periods. The computations may be executing on multiple cores in the same chip, preemptively time-shared threads on the same processor, or executed on physically separated processors.

Concurrency is a property of systems in which several computations can be in progress simultaneously, and potentially interacting with each other. The computations may be executing on multiple cores in the same chip, preemptively time-shared threads on the same processor, or executed on physically separated processors.

A number of mathematical models have been developed for general concurrent computation including Petri nets, process calculi, the Parallel Random Access Machine model, the Actor model and the Reo Coordination Language.

Issues

Because computations in a concurrent system can interact with each other while they are executing, the number of possible execution paths in the system can be extremely large, and the resulting outcome can be indeterminate. Concurrent use of shared resources can be a source of indeterminacy leading to issues such as deadlock, and starvation.

Performance

A common misconception is that increasing concurrency will always improve performance. While it can improve throughput for CPU bound processes by using more CPUs, and I/O bound tasks by amortising the latency of each task, there is many situation where more concurrency hurts performance. Even when it improves performance it can reduce maintainability.

Examples of where concurrency doesn't help

  • The overhead of using multiple threads, exceeds the potential improvement. e.g. You have a very short task, but it takes a long time to pass it (and the data associated with it) to another thread.
    • e.g. the cost of locking a resource exceeds the time taken in the protected operation. A single threaded task might perform much better (and be simpler)
  • You are already using a shared resource to it's maximum extent. e.g. all your CPUs, hard drives, network connection are fully utilised. In this case, the overhead can increase decreasing overall performance.
  • You don't need a performance increase, but adding concurrency increases the complexity of your application. A common argument is; you have to use all your CPUs because they are there (even if there is nothing to gain and everything to lose)

References

22520 questions
10
votes
2 answers

What is the point of SplittableRandom?

Java 8 added a random number generator called SplittableRandom which seems to be meant for use with streams. However, it isn't clear how it is better than or more useful than ThreadLocalRandom. From reading documentation, it seems the algorithm…
Carl Mastrangelo
  • 5,970
  • 1
  • 28
  • 37
10
votes
1 answer

ConcurrentDictionary + Lazy -- would instantiation happen only once?

Scenario Let's say we have: var dictionary = new ConcurrentDictionary>(); Instantiating Heavy is very resource-consuming. Let's consider this code: return dictionary.GetOrAdd("key", key => { return new Lazy(() => …
ebvtrnog
  • 4,167
  • 4
  • 31
  • 59
10
votes
3 answers

How can I update/insert Object into inner List in Mongodb?

Blog { id:"001" title:"This is a test blog", content:"...." comments:[{title:"comment1",content:".."},{title:"comment2",content:"..."}] } comments is a inner list in blog. But how can I retrieve only comment1? and How can I…
L.J.W
  • 1,575
  • 5
  • 18
  • 24
10
votes
2 answers

CompletableFuture multi-threaded, single thread concurrent, or both?

I just started looking into Java's CompletableFuture and a little bit confused on whether this is truly asynchronous (i.e running on one thread concurrently) or spanning multiple threads (Parallel). For example, suppose I'd like to make 1000…
lorenzocastillo
  • 985
  • 3
  • 13
  • 24
10
votes
2 answers

In Java Concurrency In Practice by Brian Goetz, why is the Memoizer class not annotated with @ThreadSafe?

Java Concurrency In Practice by Brian Goetz provides an example of a efficient scalable cache for concurrent use. The final version of the example showing the implementation for class Memoizer (pg 108) shows such a cache. I am wondering why the…
dig_dug
  • 101
  • 3
10
votes
1 answer

"The usage of semaphores is subtly wrong"

This past semester I was taking an OS practicum in C, in which the first project involved making a threads package, then writing a multiple producer-consumer program to demonstrate the functionality. However, after getting grading feedback, I lost…
Gautam Kamath
  • 203
  • 1
  • 5
10
votes
2 answers

What is fairness in multi-threading programming?

What is thread fairness or fairness in concurrent/multi-threaded programming? I have googled, there is loads of info on multi-threading but not exactly on fairness. Can some one explain. An example is most welcome.
user8205906
10
votes
1 answer

Is there a way to force parallelStream() to go parallel?

If the input size is too small the library automatically serializes the execution of the maps in the stream, but this automation doesn't and can't take in account how heavy is the map operation. Is there a way to force parallelStream() to actually…
10
votes
3 answers

Documentation contradictions about ReentrantReadWriteLock. Does eventually write lock has priority over read lock or not in fair mode?

From ReentrantLock javadoc: Fair mode When constructed as fair, threads contend for entry using an approximately arrival-order policy. When the currently held lock is released either the longest-waiting single writer thread will be…
gstackoverflow
  • 36,709
  • 117
  • 359
  • 710
10
votes
1 answer

Java8 CompletableFuture conditional chaining

I have read many java8 completable future tutorials, most of them are basically same. All talking about some basic method "thenAccept"/"thenApply"/thenCombine" to build a pipeline flow. But when come to a real work problem, I feel hard to organize…
junfei wang
  • 111
  • 1
  • 5
10
votes
13 answers

Are single-threaded applications a dead technology?

I just bought a new, sub-US$1,000 laptop, one aimed squarely at the consumer, non-developer market and, looking over the specs, was surprised to find that it came standard with a dual-core processor. This led me to the question: with multicore…
Yes - that Jake.
  • 16,725
  • 14
  • 70
  • 96
10
votes
5 answers

How to do text full history in Django?

I'd like to have the full history of a large text field edited by users, stored using Django. I've seen the projects: Django Full History (Google Code) Django ModelHistory, and Django FullHistory I've a special use-case that probably falls outside…
Brian M. Hunt
  • 81,008
  • 74
  • 230
  • 343
10
votes
1 answer

How to make concurrent network requests using OKHTTP?

I'm looking for a best practice for making concurrent network requests using the OKHTTP library. Basically here's what I want to do: I want to write a method that makes N concurrent network requests to different URLs, and return ONLY when ALL N…
Vlad
  • 8,038
  • 14
  • 60
  • 92
10
votes
4 answers

Use case for Lock.tryLock()

At API docs of Lockinterface for method tryLock(), this code sample is pasted, A typical usage idiom for this method would be: Lock lock = ...; if (lock.tryLock()) { try { // manipulate protected state } finally { …
Sabir Khan
  • 9,826
  • 7
  • 45
  • 98
10
votes
6 answers

In what circumstances lock free data structures are faster than lock based ones?

I'm currently reading C++ Concurrency in Action book by Anthony Williams and there are several lock free data structures implementations. In the forward of the chapter about lock free data structures in the book Anthony is writing: This brings us…
bobeff
  • 3,543
  • 3
  • 34
  • 62
1 2 3
99
100