What is concurrency and what are the concurrency classes? [closed]


Closed. This question should be specified . Answers to it are not accepted at the moment.

Want to improve this question? Reformulate the question so that it focuses on only one problem.

Closed 5 years ago.

Improve the question

Can you explain on your fingers what kind of beast such concurrency is and how to use it?

Author: redL1ne, 2015-09-04

2 answers

Concurrency is working with multithreaded code.

This is usually understood as:

  1. Managing (creating/starting/stopping) threads of the Java virtual machine (which are "threads"). The code in each such thread can be executed in parallel with the code in other threads. Thus, several tasks can be performed simultaneously. Here you need to understand such concepts as threads, thread pools, futures.

    Google and to read:

    • java.lang.Thread
    • java.lang.Runnable
    • java.util.concurrent.Callable
    • java.util.concurrent.Executor
    • java.util.concurrent.Executors
    • java.util.concurrent.ThreadFactory
  2. Program execution flow control (synchronization of threads). There are always situations when the code in one thread must wait for the completion of the task in another thread. This is achieved by using a variety of synchronization tools. An important problem in this context is mutual blocking of threads (deadlock), when multiple threads are waiting for something from each other.

    Google and read:

    • java.lang.Object: wait(), notify(), notifyAll()
    • java.util.concurrent.locks.*
    • java.util.concurrent.CountDownLatch
    • java.util.concurrent.Semaphore
    • java.util.concurrent.CyclicBarrier
    • java.util.concurrent.Exchanger
    • java.util.concurrent.Phaser
  3. Managing access to memory (data) in a multithreaded environment . Here it is important to understand the Java Memory Model, the visibility of variables, the atomicity of operations, and the occurrence of race states, thread-safe collections.

    Google and read:

    • Modifier volatile
    • java.lang.ThreadLocal
    • java.util.concurrent.atomic.*
    • java.util.concurrent.ConcurrentMap
    • java.util.concurrent.BlockingQueue
    • java.util.concurrent.CopyOnWriteArraySet
    • java.util.concurrent.CopyOnWriteArrayList

For a deep understanding, I recommend reading:

 11
Author: Nofate, 2015-09-04 14:50:14

Concurrency - in short, the ability to execute 2 or more code statements simultaneously. Imagine that the code is water that flows through a pipe. It is necessary to pump water from A to B (analogous to the execution of the code from the start - main to the finish - exit). Given that you can't change the size of the pipe, we just add 1 more pipe. Hence, in theory, we get a higher speed. The number of "pipes" that will be used usefully depends on the number of cores in the processor.

There are enough classes there are a lot of them and they can be found under java. util. concurrent. I also advise you to read the official documentation. As for the use, it all depends on what you are going to do it for. A primitive example:

Thread thread = new Thread(() -> {
    // код для исполнения в другом потоке
});
thread.start(); // здесь код начинает исполняться в другом потоке

An improved example would use ExecutorService or similar classes. I advise you to read here.

 3
Author: AlmasB, 2015-09-04 09:59:19