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
421
votes
23 answers

Is there an advantage to use a Synchronized Method instead of a Synchronized Block?

Can any one tell me the advantage of synchronized method over synchronized block with an example?
Warrior
  • 39,156
  • 44
  • 139
  • 214
410
votes
22 answers

What is the difference between concurrent programming and parallel programming?

What is the difference between concurrent programming and parallel programing? I asked google but didn't find anything that helped me to understand that difference. Could you give me an example for both? For now I found this explanation:…
matekm
  • 5,990
  • 3
  • 26
  • 31
405
votes
7 answers

What is a good pattern for using a Global Mutex in C#?

The Mutex class is very misunderstood, and Global mutexes even more so. What is good, safe pattern to use when creating Global mutexes? One that will work Regardless of the locale my machine is in Is guaranteed to release the mutex…
Sam Saffron
  • 128,308
  • 78
  • 326
  • 506
396
votes
3 answers

multiprocessing.Pool: When to use apply, apply_async or map?

I have not seen clear examples with use-cases for Pool.apply, Pool.apply_async and Pool.map. I am mainly using Pool.map; what are the advantages of others?
Phyo Arkar Lwin
  • 6,673
  • 12
  • 41
  • 55
379
votes
8 answers

Why use a ReentrantLock if one can use synchronized(this)?

I'm trying to understand what makes the lock in concurrency so important if one can use synchronized (this). In the dummy code below, I can do either: synchronized the entire method or synchronize the vulnerable area (synchronized(this){...}) OR…
adhg
  • 10,437
  • 12
  • 58
  • 94
378
votes
7 answers

Is there a concurrent List in Java's JDK?

How can I create a concurrent List instance, where I can access elements by index? Does the JDK have any classes or factory methods I can use?
AlikElzin-kilaka
  • 34,335
  • 35
  • 194
  • 277
376
votes
21 answers

What is the fastest way to send 100,000 HTTP requests in Python?

I am opening a file which has 100,000 URL's. I need to send an HTTP request to each URL and print the status code. I am using Python 2.6, and so far looked at the many confusing ways Python implements threading/concurrency. I have even looked at…
IgorGanapolsky
  • 26,189
  • 23
  • 116
  • 147
371
votes
9 answers

What's the difference between a Future and a Promise?

What's the difference between Future and Promise? They both act like a placeholder for future results, but where is the main difference?
Evgenij Reznik
  • 17,916
  • 39
  • 104
  • 181
364
votes
17 answers

SET NOCOUNT ON usage

Inspired by this question where there are differing views on SET NOCOUNT... Should we use SET NOCOUNT ON for SQL Server? If not, why not? What it does Edit 6, on 22 Jul 2011 It suppresses the "xx rows affected" message after any DML. This is a…
gbn
  • 422,506
  • 82
  • 585
  • 676
324
votes
17 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
  • 65,107
  • 109
  • 251
  • 387
311
votes
10 answers

Why must wait() always be in synchronized block

We all know that in order to invoke Object.wait(), this call must be placed in synchronized block, otherwise an IllegalMonitorStateException is thrown. But what's the reason for making this restriction? I know that wait() releases the monitor, but…
diy
  • 3,590
  • 3
  • 19
  • 16
311
votes
9 answers

Is "Java Concurrency In Practice" still valid?

Is Java Concurrency in Practice still valid? I am wondering whether the ideas, concepts and implementation described in the book are still compliant with the latest Java versions. I ask because the latest edition was done in 2006.
M-D
  • 10,247
  • 9
  • 32
  • 35
307
votes
5 answers

When do I need to use AtomicBoolean in Java?

How I can use AtomicBoolean and what is that class for?
yart
  • 7,515
  • 12
  • 37
  • 37
302
votes
9 answers

Does ruby have real multithreading?

I know about the "cooperative" threading of ruby using green threads. How can I create real "OS-level" threads in my application in order to make use of multiple cpu cores for processing?
skolima
  • 31,963
  • 27
  • 115
  • 151
301
votes
12 answers

Volatile boolean vs AtomicBoolean

What does AtomicBoolean do that a volatile boolean cannot achieve?
JeffV
  • 52,985
  • 32
  • 103
  • 124