Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Thread00 to realize multithreading in Java

2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

Java how to use Thread00 to achieve multithreading, I believe that many inexperienced people do not know what to do, so this paper summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.

1 concurrency and parallelism

Concurrency: refers to two or more events occurring in the same period of time.

Parallelism: when two or more events occur at the same time (simultaneously).

In the operating system, multiple programs are installed, concurrency means that there are multiple programs running at the same time macroscopically in a period of time, which in a single CPU system, there can only be one program execution at a time, that is, microscopically, these programs run alternately, but give people the impression that they are running at the same time, because the time-sharing alternating running time is very short.

In multiple CPU systems, these programs that can be executed concurrently can be assigned to multiple processors (CPU) to achieve multi-task parallel execution, that is, each processor is used to process a program that can be executed concurrently, so that multiple programs can be executed at the same time. At present, the multi-core CPU in the computer market is a multi-core processor. The more cores there are, the more programs are processed in parallel, which can greatly improve the efficiency of computer operation.

Note: computers with single-core processors must not be able to handle multiple tasks in parallel, only multiple tasks can run concurrently on a single CPU. Similarly, threads are the same, from a macro point of view to understand that threads are running in parallel, but from a micro point of view, it is serial running, that is, a thread to run, when the system has only one CPU, threads will execute multiple threads in a certain order, we call this situation thread scheduling.

2 threads and processes

Process: refers to an application running in memory, each process has an independent memory space, and an application can run multiple processes at the same time; process is also an execution process of the program, which is the basic unit for the system to run the program; the system runs a program is the process from the creation, running to the demise of a process.

Thread: a thread is an execution unit in a process, which is responsible for the execution of programs in the current process. There is at least one thread in a process. There can be multiple threads in a process, and this application can also be called a multithreaded program.

In short: there is at least one process after a program runs, and a process can contain multiple threads

We can go to the taskbar at the bottom of the computer, right-> open the task manager, and we can view the progress of the current task:

Process

Thread

Thread scheduling:

Time-sharing scheduling

All threads take turns to use the right to use CPU, evenly allocating the amount of time each thread takes up CPU.

Preemptive scheduling

Priority is given to high-priority threads using CPU. If the threads have the same priority, then one is randomly selected (thread randomness), and Java uses preemptive scheduling.

Most operating systems support multiple processes to run concurrently, and today's operating systems almost support running multiple programs at the same time. For example: now we use the editor, screencap software, drawing board, dos window and other software in class. At this point, these programs are running at the same time, "it feels like the software is running at the same time".

In fact, CPU (Central processing Unit) uses preemptive scheduling mode to switch between multiple threads at a high speed. For a core of CPU, only one thread can be executed at a time, while CPU's switching between multiple threads is faster than we think, and seems to be running at the same time. In fact, multithreaded programs can not improve the running speed of the program, but can improve the running efficiency of the program and make the utilization rate of CPU higher.

Detailed explanation of preemptive scheduling

Set the priority of the thread

3 synchronous and asynchronous

4 synchronous lock and deadlock

Synchronous locks are prone to problems when multiple threads access the same data at the same time. In order to avoid this situation, we need to ensure that threads are mutually exclusive, that is, multiple threads executing concurrently are allowed to access shared data only one thread at a time. The synchronized keyword can be used in Java to acquire a synchronization lock for an object.

What a deadlock is a deadlock is that multiple threads are blocked at the same time, and one or all of them are waiting for a resource to be released.

After reading the above, have you mastered how to use Thread00 to implement multithreading in Java? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report