Simple and intuitive - Equipment for Java multi-threaded programming (1)

xiaoxiao2021-03-06  38

Using multi-threads in the Java program is much easier than in C or C , this is because Java programming languages ​​provide language-level support. This article describes how intuitive multi-thread in the Java program via a simple programming example. After reading this article, users should be able to write simple multithreaded programs. Why is it waiting in line? The following simple Java program completes four unrelated tasks. Such a program has a single control thread that controls linearly between the four tasks. Also, because the resources required - printers, disks, databases, and display - Due to the inherent latency of hardware and software, each task contains significant wait time. Therefore, the program must wait for the printer to complete the task of the print file before accessing the database, and so on. If you are waiting for the completion of the program, this is a bad use of computing resources and your time. One way to improve this procedure is to make it a multi-thread. Four irrelevant tasks

Class myclass {

Static Public Void Main (String Args []) {

Print_a_file ();

MANipulate_another_file ();

Access_database ();

Draw_picture_on_screen ();

}

}

In this example, each task must wait for the previous task before the start, even if the task involved is not related to the task involved. However, in real life, we often use multithreaded models. We can also let children, spouses and parents complete other tasks while dealing with certain tasks. For example, I may send my son to the post office to buy a stamp. This is called a plurality of control (or execution) threads with software terms. You can use two different ways to get multiple control threads:

Multiple processes can create multiple processes in most operating systems. When a program is started, it can create a process for each task that is about to start and allows them to run at the same time. When a program is blocked by waiting for network access or user input, another program can also operate, which increases resource utilization. However, in this way, create each process to pay a certain price: Set a process to occupy a considerable part of the processor time and memory resources. Moreover, most operating systems do not allow the process to access memory space for other processes. Therefore, communication between processes is inconvenient, and it will not provide itself to an easy programming model. Thread thread is also called light processes (LWP). Because the thread can only be active in the scope of a single process, it is cheaper than the creation process. Thus, because the thread allows collaboration and data exchange, it is very cheap in the calculation resource, thread is more desirable than the process. The thread requires the support of the operating system, so it is not all the machines to provide a thread. Java programming language, as a quite new language, has been integrated with the thread support and language itself, which provides a strong support for threads.

转载请注明原文地址:https://www.9cbs.com/read-68841.html

New Post(0)