Java. Multitasking. Threads of execution (Threads). Basic concepts

Multitasking. Threads of execution (Threads). Basic concepts


Search other sites:

1. Multitasking. Types of multitasking. Process and thread based multitasking

Modern operating systems have a multitasking mechanism. With the help of this mechanism, several programs (tasks) can be executed simultaneously in the operating system. For example, you can type a Java program that uses streams while listening to Beethoven’s music.

There are two types of multitasking:

  • process-based multitasking is a means of executing two or more programs in parallel on a computer;
  • thread-based multitasking is a means of parallelizing code execution within a single program (process).

The main benefit of using multitasking is that it reduces the waiting time for multiple tasks. Reducing latency in modern interactive environments allows you to make the most of the available computing power on the system.


2. Process-based multitasking. General concepts

When using process-based multitasking, process is an important concept. A process is a separate running program. Hence, process-based multitasking is a means of executing two or more programs in parallel on a computer.

Parallel execution is provided by the processor (several processors) in such a way that a certain quantum of time (resource) of the processor is allocated to each program being executed in parallel. In this case, the program being executed is considered the smallest unit of code.

For example, developing a program using the Java compiler is a separate process that is not influenced by that program. Processes are large tasks and require more system resources. Each process is allocated its own separate address space. Communication between processes is limited and resource intensive. Switching the context (changing activity) between processes also requires “a lot of effort” from the processor and the operating system.


3. Thread-based multitasking. Thread of execution

Thread-based multitasking is also called multithreading. When using multithreading, the smallest unit of dispatch code is the thread of execution. One program can execute several threads (tasks) at the same time. Thus, thread-based multitasking is a means of breaking code into separate parallel-executing pieces.

For example, in a word processor, you can print a document at the same time as you type in another document.

Threads of execution within processes require much less system resources than processes. Threads share the same address space of one process, in which they are parallelized. Communication and context switching (changing activity) between processes relatively lightly load the processor and the operating system.


4. General information about threads of execution. Multithreading

The Java programming language contains built-in tools for implementing multi-threaded programming. With these tools, it is possible to control the so-called threads of execution.

Выполнение каждой программы может быть разбито на несколько потоков. Each thread of execution represents a separate path for code execution. Each thread of execution is created on the main thread and is called a child thread.

After creating multiple threads in the program, the main thread and child threads:

  • can last for a different period of time;
  • can end at different points in time. It is possible here that the main thread terminates before the execution of the child thread completes.

For working with threads of execution in the Java language, extensive class libraries are implemented.


5. Types of thread states

Threads of execution can be in several states:

  • the thread is running;
  • the thread is ready for execution;
  • thread is suspended;
  • the thread starts executing after it stops;
  • the thread is blocked. This is the case when a thread is waiting to receive some resource;
  • the thread is interrupted. An interrupted stream cannot be restored.


6. Threads priorities. Basic concepts. Cases of context switching

When using threads of execution, the concept of priority arises. Priority is an integer value that determines the behavior of this thread in relation to other threads. For multiple threads, the numerical priority value specifies the relative priority of one thread over others. If only one thread is running, the numerical value of the priority is irrelevant. The priority of a thread is taken into account in cases when several threads are running at the same time and a so-called context switch occurs – a transition from one thread to another.

The transition from one thread to another is performed in the following cases:

  • a case where a thread with a higher priority preempts execution of a thread with a lower priority regardless of what operations the lower priority thread is performing. In other words, the higher priority thread gets the right to execute when needed. This case demonstrates what is known as preemptive multitasking;
  • a thread voluntarily passes control to another thread. In this case, control is transferred to another thread that has the highest priority among the threads currently executing. If you voluntarily need to transfer control to threads with the same priority, then these threads share the CPU time in a circle.


7. Thread synchronization. Monitor

In multitasking environments, threads (programs) run asynchronously. Asynchrony is determined by the chaotic use of available resources by the executing thread.

For two (or more) threads running in parallel, there may be cases when it is necessary to organize synchronization between them. One example of the need to apply synchronization is to avoid conflicts between threads over shared resources. Synchronization is a mechanism for communication between two (or more) threads, following certain rules to avoid collisions, simultaneous or inconsistent use of shared resources, and the like.

For synchronization in the Java language, the use of so-called monitors has been introduced. A monitor is a thread synchronization technology that stores only one thread of execution, which is granted privileges to access a resource. This means that the monitor protects resources from their concurrent use by more than one thread of execution. When using monitor technology, there is such a thing as “thread entry into the monitor“.

If several threads simultaneously need to approach a shared resource, then only the thread that entered the monitor has access. All other threads wait until the monitor is freed (a thread that entered the monitor will leave it).

Figure 1 shows an example of monitor operation for 4 threads. The monitor is entered by Thread3. This thread has access to the shared resource. All other threads wait in the queue until Thread3 leaves the monitor.

Java. Threads. Access to a shared resource using the monitor

Figure 1. Access to a shared resource using the monitor


Related topics