Before talking about multithreading, it is necessary to clarify the differences and relationships between processes and threads.
The process is being executed on the computer.Program(An active program), and each process has its own independent memory space and a group of system resources. The memory space and resources used by these programs are independent, and each program runs for its own task without interfering with each other.
The thread concept is proposed based on the process, which makes it possible to process multiple tasks simultaneously in a process. A thread can be seen as a further subdivision of the process, that is, dividing the tasks completed by the process into smaller subtasks. Each subtask is a thread and then executes these subtasks.
A thread is a basic running unit of a process. A thread also contains a memory entry point address, an exit point address, and a thread that can be executed sequentially.CodeSequence.
An important difference between a thread and a process is that a thread cannot be executed independently and must run in an active process. A thread is a series of instruction streams that are active in the process scope. Therefore, a thread is also called a lightweight process.
1. Introduction to multithreading
The multi-thread mechanism allows multiple Instruction Streams to be executed in parallel in a program. Each instruction stream is called a thread and its execution is independent of each other.
In a single CPU computer system, the system schedules the CPU time slice accordingAlgorithmAllocated to each thread, so each thread is actually executed in time. In a multi-CPU computer system, different threads of the same program can be allocated to different CPUs for execution. The execution of multiple threads is concurrent.
In multithreading, the control flow of each thread is independent from each other. The biggest difference between multithreading and traditional single-Thread Programming is that the Code between each thread is executed in disorder. Therefore, when designing a multi-threaded program, some thread state control statements are often added to satisfy the program's requirements.
2. Multithreading
Java introduces the virtual processor technology, which combines thread support with the language runtime environment. Regardless of the system, Java provides the ability to execute multiple tasks concurrently, the comprehensive support for multithreading is also an important feature of the Java language.
In Java, the built-in Thread class is used to implement multithreading. A thread consists of three parts:
(1) the virtual processor is encapsulated in the Java. Lang. Thread class, which controls the running of the entire thread and provides support for multithreading.
(2) The program code to be executed is passed to the Thread class and is controlled by the Thread class for sequential execution.
(3) the data processed by the program is passed to the Thread class.
It should be noted that the program code can be shared by multiple threads as needed. They are independent of each other's data, the two threads can share the same piece of code when executing the instance code of the same class. Similarly, data can be shared by multiple threads as needed and independent from the code. The two threads can share the same data when accessing an object.
3. Thread class
In Java, the thread class is the main class responsible for providing thread support to other classes. The thread class is defined in the Java. lang package. Its constructor methods include the following:
Thread ();
Thread (runnable target );
Thread (runnable target, string name );
Thread (string name );
Thread (threadgroup group, string name );
Thread (threadgroup group, runnable target );
Thread (threadgroup group, runnable target, string name );
Thread (threadgroup group, runnable target, string name, long stacksize );
The parameter group indicates the thread group to which the thread belongs. The target parameter is the target object of the actual execution thread body and must implement the runnable interface. The parameter name is the thread name; the stacksize parameter is the specified stack size.
In addition, the common methods of the thread class are as follows:
(1) Public static void yield (); // pause the currently executed thread and allow other threads to execute
(2) Public static void sleep (long millis); // specifies the time for the thread to sleep.
(3) Public void start (); // Changes the thread from the new state to the runable state.
(4) Public void run (); // running thread
(5) Public final void stop (); // stop the current thread
(6) Public final void wait (); // cause the current thread to wait until it is awakened or exceeds the specified waiting time
(7) Public final Boolean isalive (); // determines whether the thread is active.
(8) Public final void setpriority (INT new); // sets the thread priority.
(9) public static thread currenthread (); // return the reference of the current execution thread object
(10) Public final void every Y (); // wake up a waiting thread
(11) Public final void policyall (); // wake up all threads in the waiting state
(To be continued ......)