Multithreading is a major feature of the Java language. multithreading refers to the situation where N execution bodies exist simultaneously and work together based on several different execution clues. Programs, processes, and threads can be understood from different perspectives. A program is a piece of static code, which can be understood as a set of computer commands. A program is a dynamic execution process, from code loading to execution. A thread is a unit smaller than a process. Multiple Threads can be generated when a process is re-executed. Each thread is also a subset from production to destruction. I personally use a metaphor that seems appropriate to me as a metaphor for the three. The QQ client is a program. logging on to a QQ client starts a process of this program. Sending a message to a friend on QQ seems like a thread in this process. I don't know if this metaphor is appropriate?
The thread is also stateful and declarative. Each Java program has a default main thread. For application applcation, the main method is a main thread. the Java language uses the Thread class and its subclass objects to represent threads. the lifecycle of a new thread is as follows:
1) New: When a Thread class or its subclass object is declared and created, the new Thread object is in the new State, and now it has the corresponding memory space and other resources.
2) Ready: After a thread in the new State is started, it will enter the thread queue to wait for the CUP service. At this time, the running conditions are met. Once it is the CPU's turn, you can start your own life cycle independently from the main thread that created it.
3) run: The ready Thread is scheduled and the processing side for obtaining the CUP enters the running state. Every Thread class and its subclass objects have an important run () method, when a thread object is scheduled for execution, it automatically calls the run () method of the object and starts executing the code from the first sentence. Therefore, the operations on the thread should be written to the run () method.
4) Blocking: A running thread cannot be executed under some circumstances. the thread enters the blocking status. At this time, it cannot enter the queuing status. The thread can continue to enter the queuing status and wait for CUP processing only when the blocking cause disappears.
5) death: a thread in the dead State does not have the ability to continue execution. The main cause of thread death is that the normal running thread has completed all the work, that is, the run () method has been executed, in addition, it is forcibly terminated in advance.
Thread Scheduling also has a priority, that is, the same priority can be processed by the CPU in advance, mainly divided into three levels, high and low. the numbers represented respectively are 10.5.1. Three constants respectively indicate that they cannot be changed. The minimum priority constant is MIN_PRIORITY. The normal priority constant is NORM_PRIORITY. The highest priority constant is MAX_PRIORITY. Generally, the priority of the main thread is normal, in addition, you can use the setPriority (int a) method of the Thread class to modify the Thread priority automatically set by the system.
Java programming implements multithreading in two ways: one is to create its own thread subclass, and the other is to implement an interface Runnable. The Thread class and its method are required for final reading in that way. The Thread class has two constructor Methods: public Thread () is used to create a Thread object. Public Thread (Runnable r) creates a Thread object, and parameter r becomes the target object to be created. This goal must implement the Runnbale interface, provide the method body of the run () method of this interface, and then operate in the method body. the thread created by using two constructor methods is a newly created State, waiting for processing. then start the start () method of the thread, start the thread object, and the thread enters the queuing state, that is, the ready state. then, the content in the thread operation run () method is processed by the system. if you want to enable the thread to enter sleep state, you can call the sleep () method, you can give a parameter, representing the sleep milliseconds. if two parameters are given, they represent the second. The terminating thread is completed using the yield () method. To determine whether the thread is destroyed, you can use the idAlive () method to determine whether the thread is alive. The Runnable interface has only one method run (). we implement this interface to write the code to be operated to this method, then, pass the instance of the class implementing the entire interface to the Therad class constructor to operate the thread.
Thread Synchronization is A problem that requires attention when re-processing the thread. The synchronization method must be modified with the synchronized keyword class. After this keyword is modified, when thread A uses this method, if other threads want to use this method, they must wait until they know that thread A has used this method. below I will write an example to illustrate thread synchronization. There are two thread accounting and cashier in this example. They share a ledger. both of them can access the ledger by using the access method. When the accountant uses the access method, the deposit record is written, and the cashier writes the money record. Therefore, the cashier is forbidden to use the account book, and vice versa. That is, one person must wait to use another person. Below I will implement this through a 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(20,8);
text2 = new TextArea(20,8);
add(text1);
add(text2);
}
public void start() {
Kuaiji.start();
Chuna.start();
}
public synchronized void Cunqu(int number) {
if(Thread.currentThread() == Kuaiji) {
for(int i=1;i <=3;i++) {
money = money + number;
try {Thread.sleep(1000);}
catch(Exception e){}
text1.append(""+money);
}
}
else if(Thread.currentThread() == Chuna) {
for(int i=1;i <=2;i++) {
money = money - number/2;
try {Thread.sleep(1000);}
catch(Exception e){}
text2.append(""+money);
}
}
}
public void run() {
if(Thread.currentThread()==Kuaiji || Thread.currentThread()==Chuna) {
for(int i=1;i <=3;i++) {
Cunqu(30);
}
}
}
}
When a thread uses a variable in the synchronous method, and this variable needs to be modified by another thread to meet the needs of this thread, you can use the wait () method in the synchronous method, wait for this thread and allow other threads to use this synchronization method. the notfyAll () method is used for all the comrades. Because the waiting thread used this synchronization method ends and waits for it to run in the synchronization method, if notfy () is used () it means that the same thread is used for synchronization. the simple understanding is that the wait () method allows the thread to wait, notfy () when a thread runs, notfyAll () allows all threads to run. Java supports multithreading. Generally, threads do not need to be processed by ourselves, but they also need to be understood and mastered. In the future, the project will have different requirements based on different situations.