concurrency

Getting started with concurrency

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.

Installation or Setup

Detailed instructions on getting concurrency set up or installed.

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.


This modified text is an extract of the original Stack Overflow Documentation created by the contributors and released under CC BY-SA 3.0 This website is not affiliated with Stack Overflow