Multitasking in Java

Thread safety

Thread safety is a property of an object or code that ensures that when executed or used by multiple threads, the code behaves as intended. For example, a thread-safe counter will not skip any count, even if the same instance of this counter will be used by multiple threads.

Difference between "concurrency" and "parallelism"

Concurrency is a way of solving many problems at the same time.

Signs:

  • The presence of several control threads (for example, Thread in Java, coroutine in Kotlin), if there is one control thread, then there can be no concurrent execution.
  • Non-deterministic execution result. The result depends on random events, implementation and how the synchronization was done. Even if each thread is fully deterministic, the final result will be non-deterministic.

Parallelism is a way of doing different parts of the same task.

Signs:

  • Optionally has multiple threads of control.
  • It can lead to a deterministic result, so, for example, the result of multiplying each element of the array by a number will not change if you multiply it in parts in parallel.

Co-op multitasking

Cooperative multitasking is a way of dividing processor time between threads, in which each thread must voluntarily surrender control to the next.

The advantages of this approach are ease of implementation, and less overhead for context switching.

Disadvantages - if one thread hangs or behaves incorrectly, then the whole system hangs and other threads will never get control.

Type of multitasking Java use

Java uses preemptive multitasking, in which the operating system decides whether to switch between threads in a process.

In contrast to cooperative multitasking, control is transferred to the operating system regardless of the state of the running applications, so that individual hung threads of a process, as a rule, do not "freeze" the entire system. Regular switching between tasks also improves the responsiveness of the application and increases the efficiency of freeing up resources that are no longer in use.

In implementation, preemptive multitasking differs from cooperative multitasking, in particular, in that it requires processing a system interrupt from a hardware timer.

ordering, as-if-serial semantics, sequential consistency, visibility, atomicity, happens-before, mutual exclusion, safe publication

ordering is a mechanism that determines when one thread can see the out-of-order (wrong) order of execution of instructions from another thread. To improve performance, the CPU can reorder processor instructions and execute them in random order until no difference is visible to the thread inside. The guarantee provided by this mechanism is called as-if-serial semantics.

sequential consistency is the same as as-if-serial semantics, guaranteeing that within a single thread the side effects of all operations will be as if all operations are performed sequentially.

visibility determines when activities in one thread are visible from another thread.

happens-before is a logical restriction on the order of execution of program instructions. If it is indicated that writing to a variable and its subsequent reading are linked through this dependency, then no matter how the instructions are reordered during execution, at the time of reading all the results associated with the write process are already fixed and visible.

atomicity - atomicity of operations. An atomic operation looks like a single and indivisible command of the processor, which can be either already completed or not yet executed.

mutual exclusion (mutual exclusion, semaphore with one state) - a mechanism that guarantees a thread exclusive access to a resource. Used to prevent concurrent access to a shared resource. Only one thread can own such a resource at a time. Simplest example: synchronized(obj) { … }.

safe publication - showing objects to other streams from the current one, without violating the visibility restrictions. Ways to post like this in Java:

  • static{} initializer
  • volatile variables
  • atomic variables
  • storing in a shared variable, correctly protected using synchronized(), synchronizers or other constructs that create a read/write memory barrier
  • final variables in a shared object that has been correctly initialized

Read also:


Comments

Popular posts from this blog

Methods for reading XML in Java

XML, well-formed XML and valid XML

ArrayList and LinkedList in Java, memory usage and speed