concurrency Getting started with concurrency

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Remarks

This section provides an overview of what concurrency is, and why a developer might want to use it.

It should also mention any large subjects within concurrency, and link out to the related topics. Since the Documentation for concurrency is new, you may need to create initial versions of those related topics.

Example of concurrent execution in Java

import java.util.stream.IntStream;

public class Concurrent {
    public static void printAndWait(String s) {
        System.out.println(s);
        try {
            Thread.sleep(1000);
        } catch (Exception e) {}
    }
    
    public static void main(String[] args) {
        Thread myThread = new Thread() {
            public void run() {
                IntStream.range(1,32)
                .forEach(x -> printAndWait(""+x));
            }
         };
         myThread.start();
         IntStream.range('a', 'z').forEach(x -> printAndWait(""+(char)x));
     }
}
 

This will produce an output of something similar to

a
1
b
2
c
3
 

and so on, though results may vary. This is because the code in myThread is executed simultaneously, in a different thread, as the main flow. That is, the range 1-32 is handled by one thread, and the range a-z is handled by another.

Since there is no synchronization between the threads, there is no guarantee which one will execute first or indeed even that they will produce a result that is perfectly intertwined.

Installation or Setup

Detailed instructions on getting concurrency set up or installed.



Got any concurrency Question?