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 parse java multithreaded programming

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

In this issue, the editor will bring you about how to analyze java multithreaded programming. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

Java basic Learning: java Multithreaded programming Video

Process: a program that is being executed. Each process execution has an execution order, which is an execution path, or a control unit. A process has at least one thread.

Thread: a separate control unit in a process. The thread controls the execution of the process.

The disadvantage of multi-process: the cost of process switching is high; the communication between processes is very inconvenient.

Multithreading: it means that multiple different threads can be run at the same time in a single program, performing different tasks, and the overhead of thread switching is small.

Life cycle of a thread

Java has done a lot of work to abstract these details. Java provides an enumerated type called Thread.State, which includes the thread state seen by the operating system. The values in Thread.State outline the life cycle of a thread.

NEW

A thread has been created, but the start () method has not been called on the thread object. All threads are in this state at the beginning.

RUNNABLE

The thread is running, or can be run when the operating system dispatches the thread.

The way Java implements memory management and concurrent programming.

BLOCKED blocking state

The thread aborts running because it is waiting to acquire a lock to enter a method or block of code declared as synchronized.

Has the operation qualification, does not have the executive power.

WAITING

The thread aborts the run because it calls the Object.wait () or Thread.join () method.

In sleep and wait, there is neither operating qualification nor executive power.

TIMED_WAITING

The thread aborts the run because it calls either the Thread.sleep () method, or the Object.wait () or Thread.join () method, and passes in a timeout.

TERMINATED

Thread execution completed. The thread object's run () method exits normally, or an exception is thrown.

Visibility and variability

In Java, each Java application thread in a process has its own stack (and local variables), but these threads share the same heap, so you can easily share objects between threads, after all, all you need to do is pass references from one thread to another.

This leads to a general design principle of Java-objects are visible by default. If I have a reference to an object, I can make a copy and give it to another thread without any restrictions. References in Java are actually type pointers to a location in memory, and all threads share the same address space, so it's natural to be visible by default.

In addition to being visible by default, Java has another feature that is important for understanding concurrency-- objects are mutable, and the contents of objects (the value of instance fields) can generally be modified. Using the final keyword, you can declare a variable or reference as a constant, but such fields are not part of the object.

The combination of these two features (cross-thread visibility and object variability) greatly increases the difficulty of understanding Java concurrent programming.

Security of concurrent programming

If we want to write correct multithreaded code, we have to make the program meet an important condition.

That is, in a program, no matter what method is called and how the application thread is scheduled by the operating system, no other object seen by an object is in an illegal or inconsistent state. Such a program can be called a safe multithreaded program.

Mutual exclusion (mutualexclusion) and state protection

This code is protected as long as the state of the object may be inconsistent during modification or reading. To protect this code, the Java platform provides only one mechanism: mutex.

Java provides developers with the synchronized keyword. This keyword can be used on code blocks or methods, and when used, the Java platform restricts access to code in code blocks or methods.

Because the synchronized keyword surrounds the code, many developers believe that Java's

Concurrency is related to code. Some materials even use the code in blocks or methods modified by synchronized

It is called the critical section, and it is also considered that the critical zone is the key to concurrency. In fact, it is not, as we will see later, its

What we need to guard against is the inconsistency of the data.

The Java platform records a special tag for each object it creates, which is called a monitor. Synchronized uses these monitors (or locks) to indicate that subsequent code can temporarily render objects into inconsistent states. A series of events occur in synchronized-decorated code blocks or methods, as detailed as follows:

When a thread needs to modify an object, it temporarily changes the object into an inconsistent state.

The thread gets the monitor, indicating that it needs to temporarily mutually exclusive store this object

The thread modifies the object. After the modification, the object is in a consistent legal state.

The thread releases the monitor.

Synchronization is an assistance mechanism for protecting the state, so it is very fragile. A defect (requires the use of

The synchronized modification method is not used), which may bring disaster to the overall security of the system.

The consequences of sex.

The use of the word synchronized as the keyword "requires temporary mutex storage" not only indicates the need to get the monitor, but also indicates that when you enter the code block, JVM will re-read the current state of the object from the main memory. Similarly, when you exit a synchronized-decorated code block or method, JVM refreshes all modified objects and stores the new state in main memory.

Volatile keyword

Java also provides another keyword for concurrent access to data-- volatile. This keyword indicates that the application code must re-read the value from main memory before using a field or variable. Similarly, after modifying a value decorated with volatile, you must save it back to main memory after the variable has been written.

One of the main uses of the volatile keyword is in the "run until closed" mode. When writing multithreaded programs, use volatile if an external user or system needs to signal to the processing thread that the thread is gracefully closed after completing the current job. This process is sometimes called the "graceful end" mode.

The above is the editor for you to share how to parse java multithreaded programming, if you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

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