JAVA Tutorial: parse Java's multithreading mechanism

Source: Internet
Author: User
JAVA Tutorial: parse the multi-thread mechanism of Java-Linux general technology-Linux programming and kernel information. The following is a detailed description. I. Differences between processes and applications
  
A Process is a concept initially defined to indicate the basic execution unit of an application in a memory environment in a multi-user, multi-task operating system environment such as Unix. Taking a Unix operating system as an example, a process is a basic component in a Unix operating system environment and a basic unit for system resource allocation. Almost all user management and resource allocation tasks completed in Unix are implemented through the control of application processes by the operating system.
  
The source programs written in C, C ++, Java, and other languages are compiled into executable files by the corresponding compiler and submitted to the computer processor for running. An application in an executable state is called a process. From the user's perspective, a process is an execution process of an application. From the core perspective of the operating system, processes represent the basic units of the memory allocated by the operating system, CPU time slice, and other resources, and are the runtime environment provided for running programs. The difference between a process and an application is that an application is stored as a static file in a hard disk or other storage space of a computer system, A process is a system resource management entity maintained by the operating system under dynamic conditions. The main features of application processes in a multitasking environment include:
 
● The process has an initial entry point for memory units during execution, and it always has an independent memory address space during process survival;
● The lifetime status of a process includes creation, readiness, running, blocking, and death;
● The Process status can be divided into user and core states based on the running commands sent to the CPU during the execution of the application process. In the user State, processes execute application commands, and in the core State, application processes execute operating system commands.
  
During Unix startup, the system automatically creates swapper, init, and other system processes to manage memory resources and schedule user processes. In a Unix environment, no matter the process created by the operating system or executed by the application, it has a unique process identifier (PID ).

   Ii. Differences between processes and Java threads

The application has an initial entry point address of the memory space during execution, a code execution sequence during execution, and a memory exit point address used to identify the end of the process, each time point in the process execution process has a unique processor command corresponding to the memory unit address.
 
The Thread defined in Java also includes a memory entry point address, an exit point address, and a code sequence that can be executed sequentially. However, the main difference between a process and a thread is that the thread cannot be executed independently and must run in an active application process, therefore, it can be defined that the thread is a concurrent sequential code stream within the program.
 
The Unix and Microsoft Windows operating systems support multi-user and multi-process concurrent execution, while the Java language supports concurrent execution of multiple execution threads within the application process. Multithreading means that multiple logical units of an application can be executed concurrently. However, multithreading does not mean that multiple user processes are being executed, and the operating system does not allocate independent system resources to each thread as an independent process. A process can create its child processes. The child process and the parent process have different executable code and data memory space. In the process used to represent the application, multiple threads share the data memory space, but each thread must have an independent execution stack and Context ).
  
Based on the preceding differences, a thread can also be called a Light Weight Process (LWP ). Different threads allow task collaboration and data exchange, making computer system resource consumption very cheap.
  
Threads must be supported by the operating system. Not all types of computers support multi-threaded applications. The Java programming language combines thread support with the language runtime environment to provide multi-task concurrent execution capabilities. This is like a person in the Process of housework, put the clothes in the washing machine, automatically wash, put the rice in the rice cooker, and then start cooking. When the food is ready, the food is cooked and the clothes are washed.
  
Note that using multithreading in applications does not increase the CPU data processing capability. Only when Java programs are divided into multiple concurrent execution threads on a multi-CPU computer or under the network computing architecture, multiple threads can be started to run simultaneously, only by running different threads in Java virtual machines based on different processors can the execution efficiency of applications be improved.

In addition, multi-threaded applications are advantageous if the application must wait for resources with slow data throughput, such as network connections or database connections. Internet-based applications must be multi-threaded. For example, when developing server-side applications that support a large number of clients, you can create an application to respond to client connection requests in the form of multiple threads, so that each connection user excludes one client connection thread. In this way, the user feels that the server only serves to connect to the user, thus shortening the server's client response time.
  
   Iii. multi-thread programming in Java
  
Using Java to implement multi-threaded applications is simple. Two methods can be used to inherit from or implement different multi-threaded applications: one is that the concurrent running objects of applications directly inherit the Java Thread class; another way is to define concurrent execution objects to implement the Runnable interface.
  
Multi-Thread Programming Method inherited from Thread class
  
A Thread class is a class defined in JDK for controlling Thread objects. It encapsulates methods used for Thread control. See the following sample code:
  
CODE: // Consumer. java
Import java. util .*;
Class Consumer extends Thread
{
Int nTime;
String strConsumer;
Public Consumer (int nTime, String strConsumer)
{
This. nTime = nTime;
This. strConsumer = strConsumer;
}
Public void run ()
{
While (true)
{
Try
{
System. out. println ("Consumer name:" + strConsumer + "\ n ");
Thread. sleep (nTime );
}
Catch (Exception e)
{
E. printStackTrace ();
}
}
}
Static public void main (String args [])
{
Consumer aConsumer = new Consumer (1000, "aConsumer ");
AConsumer. start ();
Consumer bConsumer = new Consumer (2000, "bConsumer ");
BConsumer. start ();
Consumer cConsumer = new Consumer (3000, "cConsumer ");
CConsumer. start ();
}
}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.