Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the knowledge points of java multithreading

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains the "java multithreading knowledge points", the content of the explanation is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "what are the java multithreading knowledge points" bar!

1 concurrent execution

The application can run at the same time (playing games while listening to music). In an application, different blocks can also be run at the same time. This phenomenon of multiple blocks running at the same time is called concurrent execution. In a multitasking operating system, it ostensibly supports concurrent execution, all applications are executed by CPU, and for a CPU, only one program can be run at a point in time. Because CPU runs very fast, it feels like it is executed concurrently]

2 process

In an operating system, each program executed independently can be called a process.

3 threads can also have multiple execution units running at the same time in a process. These execution units can be seen as clues to the program and are called threads. [when a java program starts, a process is generated, which by default creates a thread on which the code in the main () method runs]

4 multithreaded program

Multiple pieces of code run alternately. It seems to be executed at the same time, but in fact, like the process, it is executed by CPU in turn. ]

5 creation of threads

1. Inherit Thread class 2. Implement the Runnable interface

6 background thread

SetDaemon (true)

Newly created threads are foreground threads by default. [as long as there is a foreground thread running, the process will not end. If a process has only background threads running, the process will end]

* * 7 Life cycle and state transition of threads * *

In Java, any object has a life cycle, and threads are no exception. The entire life cycle of a thread is divided into five phases:

8 the scheduling of threads JVM allocates the right to use CPU for each thread in the program according to a specific mechanism. This mechanism is called thread scheduling. In computers, there are two models for thread scheduling: 1. Time-sharing scheduling model II. Preemptive scheduling model JVM adopts preemptive scheduling model by default, and programmers don't need to care about it in most cases.

8.1 priority of thread

The priority of a thread is expressed by a positive number between 1 and 10, and the higher the number, the higher the priority. [each thread in the ready state has its own priority] the main thread has a normal priority. (5) [you can set it through the setPriority method of the Thread class]

8.2 Thread hibernation

Sleep (long millis)

Suspend the executing thread and give up the CPU to another thread. This is possible to use the static method sleep. (this method allows the currently executing thread to pause for a period of time and enter a dormant waiting state. The sleep method declaration throws an InterruptedException exception Therefore, exceptions should be caught when the method is called. Note: the static method of sleep can only control the hibernation of currently running threads. You cannot control the sleep of other threads.

8.3 Thread concession

Yield ()

Similar to sleep, except that the yield method does not block the thread. Simply convert the thread to a ready state. Have the system's scheduler reschedule again. [when a thread calls the yield () method, only threads with the same or higher priority as the current thread can get the opportunity to execute]

8.4 Thread queue jumping

Join ()

When the join () method of another thread is called in one thread, the calling thread will be blocked until the thread joined by the join () method has finished executing.

9 premise of multi-thread synchronization: when multiple threads access the same resource, it will cause some security problems. Solution: to achieve multi-thread synchronization, that is, to restrict a resource to be accessed by only one thread at a time.

Synchronization code block Object lock = new Object (); Note: the creation code of the lock object cannot be placed in the run () method.

Synchronous method modified by synchronized allows only one thread to access the method at a time, and other threads accessing the method will block until the current thread has finished accessing the method.

Thank you for your reading, these are the contents of "java multithreaded knowledge points". After the study of this article, I believe you have a deeper understanding of what java multithreaded knowledge points have, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report