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

How to introduce the priority of Java thread synchronization

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article introduces you how to introduce the priority of Java thread synchronization, the content is very detailed, interested friends can refer to, hope to be helpful to you.

Java thread synchronization requires a lot of things when using it, so let's take a look at how to make better use of this syntax. First of all, let's take a look at the four states of the thread. I hope you will find the most of what you need.

1. New state: the thread has been created but has not been executed (start () has not been called).

2. Executable status: a thread can execute, although it is not necessarily executing. CPU time may be assigned to the thread at any time, causing it to execute.

3. Blocking state: the thread will not be allocated CPU time and cannot be executed; it may block on the Imax O or on the synchronization lock.

4. Death status: normally the return of run () causes the thread to die. Calling stop () or destroy () has the same effect, but it is not recommended, the former generates an exception, and the latter forces termination and does not release the lock.

Priority of Java thread synchronization

The priority of a thread represents the importance of the thread. When multiple threads are executable at the same time and waiting for CPU time, the thread scheduling system decides who to allocate CPU time according to the priority of each thread. The thread with higher priority has a greater chance of getting CPU time, and the thread with lower priority has a smaller chance.

Introduction to the basic code of Java thread synchronous reference

An in-depth discussion on the source code of Java thread control

Finding the answer to the problem of Java thread synchronization in practice

A probe into the mysteries in the Source Code of Java Thread Communication

How the return value of the Java thread controls its future

You can call the Thread class methods getPriority () and setPriority () to access the thread's priority, which is between 1 (MIN_PRIORITY) and 10 (MAX_PRIORITY), and the default is 5 (NORM_PRIORITY).

Java thread synchronization

Because multiple threads of the same process share the same storage space, it not only brings convenience, but also brings the serious problem of access conflict. Java language provides a special mechanism to resolve this conflict, which effectively prevents the same data object from being accessed by multiple threads at the same time.

We only need to propose a mechanism for the method, which is the synchronized keyword, which includes two uses: the synchronized method and the synchronized block.

Synchronized method: declare a synchronized method by adding the synchronized keyword to the method declaration. The synchronized method controls access to class member variables: each class instance corresponds to a lock, and each synchronized method must obtain a lock of the class instance that calls the method before it can be executed, otherwise the thread it belongs to blocks, once the method is executed, the lock is monopolized, and the lock is not released until it is returned from the method, and then the blocked thread can acquire the lock and re-enter the executable state.

This mechanism ensures that for each class instance, at most one of its member functions declared as synchronized is executable (because at most one can obtain the corresponding lock for the class instance), thus effectively avoiding access conflicts of class member variables (as long as all possible methods to access class member variables are declared as synchronized).

In Java thread synchronization, not only the class instance, but also each class corresponds to a lock, so we can also declare the static member function of the class as synchronized to control its access to the static member variables of the class.

This is the end of the priority introduction on how to synchronize Java threads. I hope the above content can be helpful to you and learn more. If you think the article is good, you can share it for more people to see.

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

Development

Wechat

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

12
Report