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--
This article shows you how to analyze the design mechanism of Java multithreaded programs, the content is concise and easy to understand, it can definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
Analysis on the Mechanism of Java Multi-thread programming
Multithreading is a major feature of Java language. Multithreading is a situation where N executors exist at the same time and work together according to several different execution clues. Programs, processes and threads can be understood from different angles. A program is a static code that can be understood as a collection of computer commands. Execution is a dynamic execution process of this program, from the loading of the code to the completion of execution. A thread is a unit smaller than a process, and multiple threads can be generated in the process of re-execution of a process, and each thread is also from production to destruction, which can be understood as a subset of progress. Personally, I use a metaphor that I think is quite appropriate to compare the three. QQ client is a program, login to a QQ is to start a process of this program, and then send messages to friends on QQ looks like a thread in this process. I wonder if this analogy is appropriate?
Threads also have state and declaration cycles. Every Java program has a default main thread. For application applcation, the main method is a main thread. Java language uses objects of the Thread class and its subclasses to represent threads. The life cycle of creating a new thread is as follows:
1) New: when an object of a Thread class or its subclass is declared and created, the new thread object is in the new state, and it already has the corresponding memory space and other resources.
2) ready: after the thread in the new state is started, it will enter the thread queue to wait for the CUP service. At this time, it has the conditions to run. Once it is CPU's turn, it can start its own life cycle independently from the main thread that created it.
3) run: the ready thread is scheduled and gets the CUP processing side into the running state, each Thread class and its subclass object has an important run () method, when the thread object is scheduled to execute, it will automatically call the object's run () method, starting from the first sentence of code. Therefore, the operation on the thread should be written to the run () method.
4) blocking: if an executing thread is unable to execute under certain circumstances. When it enters the blocking state, it cannot enter the queued state, and only when the cause of the blocking disappears, the thread can continue to enter the queued state and wait for CUP processing.
5) death: the thread in the dead state does not have the ability to continue execution. the main cause of thread death is that the normally running thread has completed all the work, that is, it has finished executing the run () method, and it has been forcibly terminated in advance.
Thread scheduling also has priority, that is to say, the same arrangement with high priority can be processed by CPU in advance, mainly divided into three levels, high, middle and low. The number represented is 10.5.1 and there are three constants that cannot be changed. The minimum priority constant is MIN_PRIORITY, the ordinary priority constant is NORM_PRIORITY, and the highest priority constant is MAX_PRIORITY. The general main thread priority is normal. In addition, the thread priority automatically set by the system can be modified through the setPriority (int a) method of the Thread class.
There are two ways to realize multithreading in Java, one is to create its own thread subclass, and the other is to implement an interface Runnable. Either way, final reading requires the use of the Thread class and its methods. The Thread class has two constructors, and public Thread () is used to create a thread object. Public Thread (Runnable r) creates a thread object, and the parameter r becomes the target object to be created. This goal must implement the Runnbale interface, give the method body of the run () method of the interface, and then operate in the method body. A thread created with two constructors is a new state, waiting to be processed. Then start the thread's start () method, start the thread object, and the thread enters the queued state, that is, the ready state. The thread then manipulates the content of the run () method, which is processed by the system. If you want to put a thread into sleep, you can call the sleep () method, which gives you a parameter that represents the millisecond of sleep. If you give two parameters to represent that second. The terminating thread is done with the yield () method. To determine whether a thread is destroyed, you can use the idAlive () method to determine whether the thread is alive or not. The Runnable interface has only one method, run () method, and we implement this interface to write the code to operate in this method, and then pass an instance of the class that implements the entire interface to the constructor of the Therad class to manipulate the thread.
Thread synchronization is a problem that needs to be paid attention to when reprocessing threads. The synchronization method should be modified with the synchronized keyword class. After being modified by this keyword, when one thread A uses this method, other threads must wait until thread A finishes using this method. Let me write an example to illustrate thread synchronization. This example has two thread accountants and cashiers who share a ledger. Both of them have access to the account book, the accountant writes the deposit record when the access method is used, and the cashier writes the withdrawal record. Therefore, the cashier is prohibited from using the account book, and vice versa. Is that one person uses another person and the other has to wait. Here's what I do with a Mini Program Applet:
Import Java.applet.*
Import Java.awt.*
Import Java.awt.event.*
Public class MyThread extends Applet implements Runnable {
Int money = 100
TextArea text1 = null
TextArea text2 = null
Thread Kuaiji = null
Thread Chuna = null
Public void init () {
Kuaiji = new Thread (this)
Chuna = new Thread (this)
Text1 = new TextArea (20pc8)
Text2 = new TextArea (20pc8)
Add (text1)
Add (text2)
}
Public void start () {
Kuaiji.start ()
Chuna.start ()
}
Public synchronized void Cunqu (int number) {
If (Thread.currentThread ()) = = Kuaiji) {
For (int iTunes 1)
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.