Java foundation – multithreading

First, let's learn about the relationship between process and thread and the difference between them.

{

1: To understand multithreading, you must first understand threads, and to understand threads, you must first understand processes, because threads depend on processes.

2: What is a process? Through the task manager, we can see the existence of the process. Through observation, we found that only the running program will have a process. Process: a running program. Process is an independent unit for system resource allocation and call. Each process has its own memory space and system resources. 3: What's the point of multiple processes? Single process computers can only do one thing, while our current computers can do many things. Example: while playing games (game process), while listening to music (music process). In other words, today's computers support multiple processes and can perform multiple tasks in a period of time. Moreover, it can improve CPU utilization. Question: do you play games while listening to music at the same time? no Because a single CPU can only do one thing at a certain point in time. When we are playing games or listening to music, the CPU is doing efficient switching between programs, which makes us feel that it is carried out at the same time. 4: What is a thread? Multiple tasks can be executed in the same process, and I can see that each task is a thread. Thread: it is the execution unit of the program and the execution path. It is the most basic unit of CPU used by programs. Single thread: if the program has only one execution path. Multithreading: if the program has multiple execution paths. 5: What's the point of multithreading? The existence of multithreading is not to improve the execution speed of the program. In fact, it is to improve the utilization of applications. The execution of the program is actually robbing the CPU resources and the execution right of the CPU. Multiple processes are grabbing this resource, and if one of them has more execution paths, it will have a higher chance to grab the execution right of the CPU. We can't guarantee which thread can grab at which time, so the execution of the thread is random.

Let's pay attention to the difference between the two words: parallelism and concurrency* The former occurs logically at the same time, which refers to running multiple programs at the same time at a certain time* The latter occurs simultaneously in physics, which means that multiple programs are run at the same time at a certain point in time.

*Operation principle of Java program: * starting the JVM by Java command is equivalent to starting a process* Then the process creates a main thread to call the main method.

Question: * is the startup of JVM virtual machine single threaded or multi-threaded* Multithreaded* The reason is that the garbage collection thread should also be started first, otherwise it is easy to overflow memory* The current garbage collection thread and the previous main thread start at least two threads. Therefore, the JVM is actually multi-threaded.

}

How to establish an execution path?

Query the API document Java Lang. thread class

There are two ways to create threads in the description of this class

1. Inherit thread class

(1). Declare the class as a subclass of thread

(2). This subclass should override the run method of the thread class

(3). To create a subclass object is to create a thread object

(4). The thread can be executed by calling the start method in the thread class, and Binghui calls the run () method

Eg: a thread that calculates a prime number greater than a specified value can be written as:

Then, a thread is created and started with the following code:

After start () starts the thread, it will execute the run () method Note that the run () method stores the code to be run by the thread; So, remember that the task code of the custom thread is stored in the run () method

2. Declare and implement the runnable interface

(1). Define a class to implement runnable

(2). Override the run method in the runnable interface, and store the task code to be run by the thread in this method first

(3). Describes the thread class to create a thread object, and passes the object that implements the runnable interface as a parameter of the constructor of the thread class

(4). Call the start method of thread class to start the thread

The following code then creates and starts a thread:

At: what is the difference between calling the start method and calling the run method?

Calling start will open the thread and let the opened thread execute the thread task in the run method,

The run method is called directly, and the thread is not opened. Only the main thread executes the run method.

-----------------------------------------------Method 1: inherit thread----------------------------------------------------------------

/** how do I get the name of the thread object* Public final string getname(): get the name of the thread* How to set the name of the thread object* Public final void setname (string name): set the name of the thread. * how to get the thread object name in a subclass that is not a thread class* Public static thread currentthread(): returns the currently executing thread object * thread currentThread(). getName() */

/** public final void setdaemon (Boolean on): mark the thread as a daemon thread or user thread* When all running threads are daemon threads, the Java virtual machine exits. This method must be called before starting the thread. * * /

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>