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
10
votes
2 answers

Why this Scala code execute two Futures in one thread?

I've been using multiple threads for a long time, yet can not explain such a simple case. import java.util.concurrent.Executors import scala.concurrent._ implicit val ec = ExecutionContext.fromExecutor(Executors.newFixedThreadPool(1)) def addOne(x:…
WeiChing 林煒清
  • 4,452
  • 3
  • 30
  • 65
10
votes
5 answers

asyncio: running task only if all other tasks are awaiting

I am currently running some endless tasks using asyncio.wait I need a special function to run when all the others are on await import asyncio async def special_function(): while True: # does some work, # Passes control back to…
Zak Stucke
  • 432
  • 6
  • 18
10
votes
2 answers

Equivalent of Akka but for .NET (Concurrency Framework)

Is there something equivalent of Akka but for .NET? http://doc.akka.io/use-cases
user310291
  • 36,946
  • 82
  • 271
  • 487
10
votes
2 answers

How to run tasks concurrently in asyncio?

I'm trying to learn how to run tasks concurrently using Python's asyncio module. In the following code, I've got a mock "web crawler" for an example. Basically, I am trying to make it where there are a max of two active fetch() requests happening at…
J. Taylor
  • 4,567
  • 3
  • 35
  • 55
10
votes
1 answer

Should JavaDelegate classes for Camunda BPM be thread safe?

The main question is about static fields and singleton instances (for configs, etc.) - are instances of one process running in different threads, as usual servlet requests? If look deeper - do different @ProcessApplication run in one JVM and will…
Dan Brandt
  • 605
  • 1
  • 7
  • 21
10
votes
3 answers

Is the return statement atomic?

I pasted some code about Java concurrency: public class ValueLatch { @GuardedBy("this") private T value = null; private final CountDownLatch done = new CountDownLatch(1); public boolean isSet() { return (done.getCount() ==…
Li Yunlei
  • 263
  • 1
  • 9
10
votes
2 answers

Is it possible to terminate a promise's code block from another promise?

I wrote this test program: await Promise.anyof( Promise.allof((^5).map: {start { sleep 10; say "done $_" } }), Promise.in(5).then: { say 'ouch' } ); sleep 10; When the second promise times out it prints 'ouch' and the await exits, but the first…
Fernando Santagata
  • 1,487
  • 1
  • 8
  • 15
10
votes
3 answers

What is the difference between thread safe and thread compatible?

What is the difference between thread safe and thread compatible? What thread compatible mean? What is use cases for thread compatible? UPD: I have found this definition in the grpc documentation of StreamObserver. Also, I have found the link to…
mkUltra
  • 2,828
  • 1
  • 22
  • 47
10
votes
7 answers

java hash map thread visibility

I am fully loading a java HashMap on initialization, but after initialization multiple threads will be reading the data from the HashMap. i'd like to avoid any type of synchronization since the map is essentially read only and never changes. But…
richs
  • 4,699
  • 10
  • 43
  • 56
10
votes
1 answer

When is "race" worthwhile in Perl 6?

race divides operations on an iterable automatically into threads. For instance, (Bool.roll xx 2000).race.sum would automatically divide the sum of the 2000-long array into 4 threads. However, benchmarks show that this is much slower than if race…
jjmerelo
  • 22,578
  • 8
  • 40
  • 86
10
votes
3 answers

Immutability of string and concurrency

Should we synchronize on writing strings? Since string is immutable we will never get inconsistent state between write and read from the 2 different threads, right? On other words, why we don't have atomic for string type?
Rudziankoŭ
  • 10,681
  • 20
  • 92
  • 192
10
votes
2 answers

Are Semaphore P and V operations atomic?

Are the P() and V() operations that can be performed on a semaphore guarantee atomic? Can a semaphore prevent two processes getting into the P()?
Aditya369
  • 545
  • 1
  • 7
  • 27
10
votes
1 answer

Why use ConcurrentLinkedQueue when we have LinkedBlockingQueue?

Why would I use ConcurrentLinkedQueue when I have LinkedBlockingQueue? I know ConcurrentLinkedQueue is non blocking but LinkedBlockingQueue can be worked as ConcurrentLinkedQueue. I would use put()/offer() method for insertion and poll() method for…
sdindiver
  • 491
  • 1
  • 5
  • 19
10
votes
4 answers

Complete list of all different Intl.NumberFormats

I'm looking for a complete list of all the different Intl.NumberFormats. The Intl.NumberFormat page shows 3 different formats. ja-JP -> "¥123,457" de-DE -> 123.456,79 € en-IN -> 1,23,000 Is this all? Or is there more?
Peter
  • 11,413
  • 31
  • 100
  • 152
10
votes
6 answers

Does a running thread in an object prevent it from being garbage collected in java?

Given the code: new Thread(new BackgroundWorker()).start(); Intuitively it feels like the BackgroundWorker instance should be safe from GC until the thread exits, but is this the case ? And why ? Edit: All this heat is basically generated by me…
krosenvold
  • 75,535
  • 32
  • 152
  • 208