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

20138 questions
264
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…
Matthias
  • 41,630
  • 28
  • 100
  • 129
261
votes
5 answers

When do I need to use AtomicBoolean in Java?

How I can use AtomicBoolean and what is that class for?
yart
  • 6,695
  • 10
  • 33
  • 36
248
votes
12 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.
  • 17,929
  • 27
  • 89
  • 147
247
votes
21 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
  • 38,492
  • 24
  • 114
  • 205
236
votes
15 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
  • 9,360
  • 14
  • 52
  • 77
233
votes
11 answers

What is a coroutine?

What is a coroutine? How are they related to concurrency?
yesraaj
  • 42,284
  • 65
  • 185
  • 246
226
votes
13 answers

What is the difference between concurrency, parallelism and asynchronous methods?

Concurrency is having two tasks run in parallel on separate threads. However, asynchronous methods run in parallel but on the same 1 thread. How is this achieved? Also, what about parallelism? What are the differences between these 3 concepts?
GurdeepS
  • 58,809
  • 95
  • 236
  • 371
222
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,050
  • 7
  • 38
  • 74
209
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
  • 9,743
  • 18
  • 51
  • 71
205
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,347
  • 8
  • 35
  • 70
194
votes
10 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
  • 73,989
  • 165
  • 410
  • 667
192
votes
49 answers

What is the most frequent concurrency issue you've encountered in Java?

This is a poll of sorts about common concurrency problems in Java. An example might be the classic deadlock or race condition or perhaps EDT threading bugs in Swing. I'm interested both in a breadth of possible issues but also in what issues are…
Alex Miller
  • 65,227
  • 26
  • 112
  • 160
187
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,448
  • 2
  • 18
  • 16
184
votes
8 answers

How do synchronized static methods work in Java and can I use it for loading Hibernate entities?

If I have a util class with static methods that will call Hibernate functions to accomplish basic data access. I am wondering if making the method synchronized is the right approach to ensure thread-safety. I want this to prevent access of info to…
tomato
  • 5,524
  • 13
  • 41
  • 48
176
votes
8 answers

Executors.newCachedThreadPool() versus Executors.newFixedThreadPool()

newCachedThreadPool() versus newFixedThreadPool() When should I use one or the other? Which strategy is better in terms of resource utilization?
hakish
  • 3,730
  • 7
  • 36
  • 55