The passage is divided into two parts. The first part is mainly about 1-4 points
-
- The concept of multithreading
- 2. Running principle of Java program
-
- Two ways to implement multithreading
- 4.2 Differences between the methods
-
- Thread synchronization (synchronization lock)
-
- Synchronous communication between threads
1. The concept of multithreading (1) concurrency and parallelism parallelism: multiple CPU instances or multiple machines executing a piece of processing logic at the same time, is true simultaneity. Concurrency: With the CPU scheduling algorithm, the user appears to execute simultaneously, which is not really simultaneous from the CPU operation level. Concurrency often requires common resources, the processing of common resources and coordination between threads is the difficulty of concurrency. (2) What is a process process: An application is a process. Multiprocess: In an operating system, the ability to run more than one task at a time (concurrently). (3) What is a thread thread: is the operating system can carry out the smallest operation scheduling unit. Multithreading: A process can have multiple concurrent threads, each performing different tasks in parallel and independently (improving program efficiency). Multi-threaded application scenario: the server processes multiple client requests at the same time, and the computer control software shares the screen to multiple computers at the same time.
2. How Java programs work Java commands start the Java VIRTUAL Machine (JVM), which is equivalent to starting an application, that is, starting a process. The process automatically starts a “main thread”, which then calls the main method of a class. JVM startup at least starts the garbage collection thread and the main thread, so it is multithreaded.
3. Two ways to achieve multithreading
- (1). Inherit the Thread class
- (2). Implement Runnable interface
** (1). Inherit the Thread class
- Define classes that inherit Thread
- Override the run method
- Write what the new thread should do in the run method
- Creating a thread object
- Start a new thread
Here are four demos to improve understanding **
Demo1
Demo2
Demo3
Demo4
** (2) Implement the Runnable interface
- Define classes that implement the Runnable interface
- Implementing the Run method
- Write what the new thread should do in the run method
- Create a custom Runnable subclass object
- Create the Thread object and pass in the Runnable
- Call start () to start a new thread **
Here are two demos to help you understand
Demo5
Demo6
4. Two ways to achieve multithreadingThe difference between
Inheriting the Thread:
Benefits: You can use the methods in the Thread class directly, and the code is simple.
Disadvantages: You can’t use this method if you already have a parent class.
Implement the Runnable interface
Benefits: It doesn’t matter if the thread class you define already has a parent class, the interface can be implemented more than once.
Disadvantages: Can not directly use the method of Thread, need to obtain the Thread object first, to get the method of Thread, code complex
5.6 See Java Programming Basics (II);