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
195
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,990
  • 7
  • 39
  • 56
195
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
  • 69,183
  • 25
  • 122
  • 167
187
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,644
  • 13
  • 43
  • 48
180
votes
14 answers

Java concurrency: Countdown latch vs Cyclic barrier

I was reading through the java.util.concurrent API, and found that CountDownLatch: A synchronization aid that allows one or more threads to wait until a set of operations being performed in other threads completes. CyclicBarrier: A…
daydreamer
  • 87,243
  • 191
  • 450
  • 722
174
votes
14 answers

What is a database transaction?

Can someone provide a straightforward, yet not oversimplified explanation of a transaction as applied to computing?
Vlad Gudim
  • 23,397
  • 16
  • 69
  • 92
172
votes
12 answers

Good example of livelock?

I understand what livelock is, but I was wondering if anyone had a good code-based example of it? And by code-based, I do not mean "two people trying to get past each other in a corridor". If I read that again, I'll lose my lunch.
Alex Miller
  • 69,183
  • 25
  • 122
  • 167
171
votes
5 answers

Is iterating ConcurrentHashMap values thread safe?

In javadoc for ConcurrentHashMap is the following: Retrieval operations (including get) generally do not block, so may overlap with update operations (including put and remove). Retrievals reflect the results of the most recently completed update…
Palo
  • 10,591
  • 7
  • 28
  • 34
171
votes
12 answers

How to asynchronously call a method in Java

I've been looking at Go's goroutines lately and thought it would be nice to have something similar in Java. As far as I've searched the common way to parallelize a method call is to do something like: final String x = "somethingelse"; new Thread(new…
Felipe Hummel
  • 4,674
  • 5
  • 32
  • 35
169
votes
12 answers

Is it safe to get values from a java.util.HashMap from multiple threads (no modification)?

There is a case where a map will be constructed, and once it is initialized, it will never be modified again. It will however, be accessed (via get(key) only) from multiple threads. Is it safe to use a java.util.HashMap in this way? (Currently,…
Dave L.
  • 43,907
  • 11
  • 63
  • 62
168
votes
5 answers

When to use volatile with multi threading?

If there are two threads accessing a global variable then many tutorials say make the variable volatile to prevent the compiler caching the variable in a register and it thus not getting updated correctly. However two threads both accessing a shared…
David Preston
  • 2,001
  • 2
  • 13
  • 11
166
votes
4 answers

How to use the CancellationToken without throwing/catching an exception?

Compared to the preceding code for class RulyCanceler, I wanted to run code using CancellationTokenSource. How do I use it as mentioned in Cancellation Tokens, i.e. without throwing/catching an exception? Can I use the IsCancellationRequested…
Fulproof
  • 4,466
  • 6
  • 30
  • 50
164
votes
4 answers

Difference between DispatchQueue.main.async and DispatchQueue.main.sync

I have been using DispatchQueue.main.async for a long time to perform UI related operations.

 Swift provides both DispatchQueue.main.async and DispatchQueue.main.sync, and both are performed on the main queue.

 Can anyone tell me the difference…
Aman.Samghani
  • 2,151
  • 3
  • 12
  • 27
163
votes
1 answer

What's the status of multicore programming in Haskell?

What's the status of multicore programming in Haskell? What projects, tools, and libraries are available now? What experience reports have there been?
158
votes
6 answers

What's a monitor in Java?

What's a monitor referred to in concurrent programming in Java? When I read that "every object has associated a monitor" what does it meaning? Is it a special object?
xdevel2000
  • 20,780
  • 41
  • 129
  • 196
157
votes
4 answers

Different types of thread-safe Sets in Java

There seems to be a lot of different implementations and ways to generate thread-safe Sets in Java. Some examples include 1) CopyOnWriteArraySet 2) Collections.synchronizedSet(Set set) 3) ConcurrentSkipListSet 4) Collections.newSetFromMap(new…
Ben
  • 2,430
  • 3
  • 22
  • 24