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
391
votes
8 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
  • 121,058
  • 74
  • 309
  • 495
381
votes
14 answers

What is a semaphore?

A semaphore is a programming concept that is frequently used to solve multi-threading problems. My question to the community: What is a semaphore and how do you use it?
bmurphy1976
  • 25,066
  • 11
  • 29
  • 23
360
votes
18 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,490
  • 3
  • 24
  • 31
349
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
  • 394,550
  • 75
  • 549
  • 647
338
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
  • 8,947
  • 8
  • 51
  • 88
325
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
  • 5,682
  • 11
  • 35
  • 52
323
votes
17 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
  • 23,124
  • 17
  • 109
  • 132
313
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
  • 16,046
  • 33
  • 87
  • 157
309
votes
8 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
  • 30,165
  • 25
  • 168
  • 248
298
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
  • 9,777
  • 9
  • 27
  • 35
297
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
  • 29,517
  • 26
  • 108
  • 146
286
votes
6 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
  • 9,399
  • 7
  • 36
  • 50
281
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,270
  • 3
  • 16
  • 15
275
votes
8 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
  • 13,342
  • 20
  • 59
  • 92
264
votes
11 answers

Volatile boolean vs AtomicBoolean

What does AtomicBoolean do that a volatile boolean cannot achieve?
JeffV
  • 47,302
  • 31
  • 96
  • 120