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
1541
votes
41 answers

What is the difference between concurrency and parallelism?

What is the difference between concurrency and parallelism?
StackUnderflow
  • 24,080
  • 14
  • 54
  • 77
1485
votes
24 answers

How do I use threading in Python?

I would like a clear example showing tasks being divided across multiple threads.
albruno
  • 14,955
  • 3
  • 18
  • 6
1304
votes
18 answers

What is a race condition?

When writing multithreaded applications, one of the most common problems experienced is race conditions. My questions to the community are: What is the race condition? How do you detect them? How do you handle them? Finally, how do you prevent them…
bmurphy1976
  • 29,564
  • 11
  • 33
  • 24
1169
votes
16 answers

Collection was modified; enumeration operation may not execute

I can't get to the bottom of this error, because when the debugger is attached, it does not seem to occur. Collection was modified; enumeration operation may not execute Below is the code. This is a WCF server in a Windows service. The method…
cdonner
  • 37,019
  • 22
  • 105
  • 153
973
votes
26 answers

When and how should I use a ThreadLocal variable?

When should I use a ThreadLocal variable? How is it used?
929
votes
10 answers

What is a mutex?

A mutex is a programming concept that is frequently used to solve multi-threading problems. My question to the community: What is a mutex and how do you use it?
bmurphy1976
  • 29,564
  • 11
  • 33
  • 24
684
votes
10 answers

Why there is no ConcurrentHashSet against ConcurrentHashMap

HashSet is based on HashMap. If we look at HashSet implementation, everything is been managed under HashMap. is used as a key of HashMap. And we know that HashMap is not thread safe. That is why we have ConcurrentHashMap in…
Talha Ahmed Khan
  • 15,043
  • 10
  • 42
  • 49
681
votes
13 answers

Is JavaScript guaranteed to be single-threaded?

JavaScript is known to be single-threaded in all modern browser implementations, but is that specified in any standard or is it just by tradition? Is it totally safe to assume that JavaScript is always single-threaded?
Egor Pavlikhin
  • 17,503
  • 16
  • 61
  • 99
648
votes
19 answers

What's the difference between ConcurrentHashMap and Collections.synchronizedMap(Map)?

I have a Map which is to be modified by several threads concurrently. There seem to be three different synchronized Map implementations in the Java API: Hashtable Collections.synchronizedMap(Map) ConcurrentHashMap From what I understand,…
Henning
  • 11,496
  • 5
  • 27
  • 25
640
votes
10 answers

What is the difference between lock, mutex and semaphore?

I've heard these words related to concurrent programming, but what's the difference between lock, mutex and semaphore?
victor
  • 6,411
  • 3
  • 16
  • 5
490
votes
9 answers

NSOperation vs Grand Central Dispatch

I'm learning about concurrent programming for iOS. So far I've read about NSOperation/NSOperationQueue and GCD. What are the reasons for using NSOperationQueue over GCD and vice versa? Sounds like both GCD and NSOperationQueue abstract away the…
480
votes
16 answers

Custom thread pool in Java 8 parallel stream

Is it possible to specify a custom thread pool for Java 8 parallel stream? I can not find it anywhere. Imagine that I have a server application and I would like to use parallel streams. But the application is large and multi-threaded so I want to…
Lukas
  • 13,606
  • 9
  • 31
  • 40
460
votes
16 answers

What is the meaning of the term "thread-safe"?

Does it mean that two threads can't change the underlying data simultaneously? Or does it mean that the given code segment will run with predictable results when multiple threads are executing that code segment?
456
votes
27 answers

How to wait for all threads to finish, using ExecutorService?

I need to execute some amount of tasks 4 at a time, something like this: ExecutorService taskExecutor = Executors.newFixedThreadPool(4); while(...) { taskExecutor.execute(new MyTask()); } //...wait for completion somehow How can I get notified…
serg
  • 109,619
  • 77
  • 317
  • 330
425
votes
15 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
  • 29,564
  • 11
  • 33
  • 24
1
2 3
99 100