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
292
votes
7 answers

How is Node.js inherently faster when it still relies on Threads internally?

I just watched the following video: Introduction to Node.js and still don't understand how you get the speed benefits. Mainly, at one point Ryan Dahl (Node.js' creator) says that Node.js is event-loop based instead of thread-based. Threads are…
Ralph Caraveo
  • 10,025
  • 7
  • 40
  • 52
287
votes
9 answers

Volatile vs Static in Java

Is it correct to say that static means one copy of the value for all objects and volatile means one copy of the value for all threads? Anyway a static variable value is also going to be one value for all threads, then why should we go for volatile?
Jothi
  • 14,720
  • 22
  • 68
  • 93
280
votes
13 answers

Practical uses for AtomicInteger

I sort of understand that AtomicInteger and other Atomic variables allow concurrent accesses. In what cases is this class typically used though?
James P.
  • 19,313
  • 27
  • 97
  • 155
278
votes
14 answers

What is a coroutine?

What is a coroutine? How are they related to concurrency?
yesraaj
  • 46,370
  • 69
  • 194
  • 251
265
votes
12 answers

Is AsyncTask really conceptually flawed or am I just missing something?

I have investigated this problem for months now, came up with different solutions to it, which I am not happy with since they are all massive hacks. I still cannot believe that a class that flawed in design made it into the framework and no-one is…
mxk
  • 43,056
  • 28
  • 105
  • 132
265
votes
24 answers

What is the Swift equivalent to Objective-C's "@synchronized"?

I've searched the Swift book, but can't find the Swift version of @synchronized. How do I do mutual exclusion in Swift?
Bill
  • 44,502
  • 24
  • 122
  • 213
251
votes
14 answers

What's the difference between Thread start() and Runnable run()

Say we have these two Runnables: class R1 implements Runnable { public void run() { … } … } class R2 implements Runnable { public void run() { … } … } Then what's the difference between this: public static void main() { R1 r1 =…
Ori Popowski
  • 10,432
  • 15
  • 57
  • 79
226
votes
7 answers

What is the Haskell response to Node.js?

I believe the Erlang community is not envious of Node.js as it does non-blocking I/O natively and has ways to scale deployments easily to more than one processor (something not even built-in in Node.js). More details at…
gawi
  • 13,940
  • 7
  • 42
  • 78
225
votes
9 answers

What is the difference between a thread and a fiber?

What is the difference between a thread and a fiber? I've heard of fibers from ruby and I've read heard they're available in other languages, could somebody explain to me in simple terms what is the difference between a thread and a fiber.
tatsuhirosatou
  • 25,149
  • 14
  • 39
  • 40
214
votes
5 answers

How does LMAX's disruptor pattern work?

I am trying to understand the disruptor pattern. I have watched the InfoQ video and tried to read their paper. I understand there is a ring buffer involved, that it is initialized as an extremely large array to take advantage of cache locality,…
Shahbaz
  • 10,395
  • 21
  • 54
  • 83
211
votes
11 answers

Synchronization vs Lock

java.util.concurrent API provides a class called as Lock, which would basically serialize the control in order to access the critical resource. It gives method such as park() and unpark(). We can do similar things if we can use synchronized…
daydreamer
  • 87,243
  • 191
  • 450
  • 722
210
votes
5 answers

Why does this Java program terminate despite that apparently it shouldn't (and didn't)?

A sensitive operation in my lab today went completely wrong. An actuator on an electron microscope went over its boundary, and after a chain of events I lost $12 million of equipment. I've narrowed down over 40K lines in the faulty module to…
Dog
  • 7,707
  • 8
  • 40
  • 74
203
votes
6 answers

Why is creating a Thread said to be expensive?

The Java tutorials say that creating a Thread is expensive. But why exactly is it expensive? What exactly is happening when a Java Thread is created that makes its creation expensive? I'm taking the statement as true, but I'm just interested in…
kachanov
  • 2,696
  • 2
  • 21
  • 16
203
votes
3 answers

Is HttpClient safe to use concurrently?

In all the examples I can find of usages of HttpClient, it is used for one off calls. But what if I have a persistent client situation, where several requests can be made concurrently? Basically, is it safe to call client.PostAsync on 2 threads at…
Alex K
  • 10,835
  • 8
  • 29
  • 34
199
votes
18 answers

What is a deadlock?

When writing multi-threaded applications, one of the most common problems experienced are deadlocks. My questions to the community are: What is a deadlock? How do you detect them? Do you handle them? And finally, how do you prevent them from…
bmurphy1976
  • 29,564
  • 11
  • 33
  • 24