In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
How to analyze the thread form of Java language specification, I believe that many inexperienced people do not know what to do about it. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
When we first came into contact with Java, we didn't pay much attention to the Java language specification, which was wrong. Let's see that after we start executing the Java language specification program, at least one thread starts to operate, the one called the main thread, which executes the main () of the input class. When all processing in main () ends, the main thread ends at the same time.
When the size of the application is large enough, the multithreading in the program exists in each form. Here are a few common examples:
1) GUI application.
2) compare the time-consuming Istroke O treatment.
3) multiple clients.
If you want to start a thread, there are two ways:
1) start the thread using an instance of a subclass of the Thread class.
2) start the thread by using the instance of the implementation class of the Runnable interface.
It is important to note that "instance of Thread" and "thread itself" are two different parts. Even if an instance of Thread is created, the thread has not been started, and even if the thread has ended, the Thread instance will not just disappear.
Create a class that implements the Runnable interface, pass an instance of this class to the constructor of Thread, and call start (). This is the way to start a thread using the Runnable interface.
Remember: either way, the method to start a new thread is always the start () of the Thread class.
Common string considerations in Java language
Analysis of Java language and C # language
Java language Learning: an Overview
What is OSGi: the dynamic Modular system of Java language
Quickly master the methods of PHP calling Java language
Thread execution can be suspended using the sleep () of the Thread class. When you execute the following statement, you can temporarily stop the current thread (the thread executing the statement) from crossing the 1000ms.
Thread.sleep (1000)
When instance methods are declared with the keyword synchronized, you can let only one thread operate on such methods of a certain type of instance. "Let one thread operate" does not mean that only a particular thread can operate, but that it can only be executed by one thread at a time. This method is called the synchronized method (synchronous method).
The synchronized instance method uses this locks to do thread sharing mutexes. The synchronized class method uses the locking of the class object of the class to do thread sharing mutexes.
The Java language regulates thread coordination (these three methods are executed only if the executing thread has a lock on obj in its hand):
1) obj.wait () is the wait set that puts the current thread into obj
2) obj.notify () wakes up a thread from the wait set of obj
3) obj.notifyAll () wakes up all threads in obj's wait set.
The awakened thread is only waiting for a lock (but no longer in the wait set), and the lock of the obj at that time is still in the hands of the thread executing notify () or notifyAll (). When the awakened thread gets the lock, execution continues after wait (). Introduction
The Evaluation Standard of Java language Specification for multithreaded programs
Security-does not damage the object.
Survivability-carry out the necessary processing. (liveness)
Reusability-reusable classes.
Sexual energy-can be processed quickly and in large quantities.
Single Threaded Execution-only one person can cross the bridge
Single Threaded Execution means "execute as one thread". Just as a thin log bridge can only allow one person to pass, this mode is used to restrict the running of only one thread.
In Single Threaded Execution Pattern, we defend unsafeMethod so that only one thread can call it at the same time (plus synchronized). The scope of the program that must be executed by a single thread is called a "critical section".
When using Single Threaded Execution Pattern, there may be a risk of deadlock.
When the fields of SharedResource are open to subclass access, security may be lost because the subclass writes unsafeMethod.
The synchronized method and synchronized blocks do unlock whether they encounter a return or an exception.
Conclusion: in the Java language specification:
1) the assignment of basic types, reference types, and references are atomic operations.
2) but the designation and reference of long and double can be separated.
3) when you want to share long or double fields between threads, you must operate in synchronized or declare it as volatile.
After reading the above, have you mastered how to analyze the thread form of the Java language specification? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.