1. Thread and Process Concepts
1.1. Process: Has a separate memory space, each independently executed program is called a process
? 1.2. Thread: A thread is an execution path inside a program, and a Java Virtual machine allows an application to run multiple execution threads concurrently
? 1.3. The difference between threads and processes
Each process has a separate code and data space (process context), and the switching overhead between processes is large
Threads: Threads in the same process share code and data space with little overhead for thread switching
Multi-process: multiple tasks (Programs) can be run concurrently in the operating system
Multithreading: Executing multiple execution paths concurrently in the same application
2. Creating and starting a thread
First: Inherit the thread class, overriding the Run method
Second: Implementing the Runnable interface, overriding the Run method
3. Comparison of two ways to create
? 3.1 using the Runnable interface
Can also inherit from other classes;
Maintain the consistency of the program style.
? 3.2 Direct inheritance of the thread class
Can no longer inherit from other classes;
Simple to write, can manipulate threads directly
? When you create a thread by implementing the Runnable interface, you can provide shared data for multiple threads of the same program code.
4. Thread Summary
4.1.Java threads are implemented through the Java.lang.Thread class.
? 4.2. When the program starts running, the JVM generates a thread (the main thread), and the Main method (the main method) runs on that thread.
? 4.3. You can create a new thread by creating an instance of the thread.
A. Each thread completes its operation by means of the method run () that corresponds to a particular thread object, and the Method run () is called the thread body .
B. Start a thread by calling the start () method of the thread class. The thread enters the runnable (operational) state, and it goes to the thread
Scheduler registers this thread.
C. Calling the start () method does not necessarily execute the thread immediately, as stated above, it simply enters runnable instead of running.
? Note that you do not call the thread's run () method directly in the program.
5. Basic use of threading methods
java-multithreaded Articles