(Memories from last season:
Two termination methods for multithreading:
·Thread
·Runnable
After all, it is requested to use the start() method in the Thread class to start multi-threading
Top knowledge points this season:
thread control method
state transitions of threads
All threads are actually started together, but the order of preempting CPU resources is different.
Thread title setting and acquisition
All operations in a thread are in one class - the Thread class.
Judging from the provided methods: if getName and setName are provided in multi-threading, it should be inferred that there must be a name attribute in the Thread class.
ThreadDemo01:
Operational results:
In the program, we did not set the Thread-0 title, let's set its title below
Verify the effect again
The main thread is called by the target, so it is controlled by the main thread
From the above title we should realize that all the JAVA programs we have run before are running on threads.
java class name --gt; the process that started the JVM
The main method inside is actually a thread divided on the JVM process.
If no name is set for the thread, the default name of the thread is Thread-x (x: indicates a number, starting from 0)
If the count is increased by 1, then all objects must share the same variable, then this variable must be of static type.
Take a look at the effect:
question:
In a JAVA program, what is the minimum number of threads to start each run?
The main thread (main)
Garbage collection thread (GC)
When the name was just set, the name was set after the target of the Thread class was created, and the thread name can be set at the structure method: publicThread(Runnabletarget, Stringname);
The result is out
Basic method of thread control
The method name starts with isXxx and usually returns a boolean type.
activate thread
The thread is started by the start() method. It must be dead before the thread is started, but how can it be judged after the thread is started?
This program can better verify one point - the output of multi-threaded programs is not fixed, and whoever comes first should be fully considered.
set background thread
Under Linux, you can see that there are background threads very cleanly.
What if you don't set a background thread, look at the normal startup thread
The program enters an infinite loop
The program is now set to run in the background
We found that the program ran in the background after running for a period of time, and the program was invisible in the foreground
thread sleep
If the good sleep method provided in the Thread class is not used now, how can the thread temporarily stop running?
Found that the program runs a little faster, we add a for empty loop
Now you can clean up and see that the programs are running one by one
A special sleep method is provided in the multi-threaded class - sleep
publicstaticvoidsleep(longmillis)--gt; sleep milliseconds
throwsInterruptedException--gt; throws an aborted exception
The program achieves the same effect~Three threads jump out at the same time~This is a multi-threaded sleep operation
Forced execution of threads
publicfinalvoidjoin()--gt; confirms that this method cannot be overridden by subclasses
throwsInterruptedException
Let's take a look at this t.txt file~ When the i value is 100, the Thread-0 thread is forcibly executed
Summarize
1. Get the current thread:
publicstaticThreadcurrentThread();
2. Get the name of the thread:
publicStringgetName();
3. Set the name of the thread:
publicvoidsetName(Stringname)
There is also support for structural methods:
-publicThread(Stringname); set the name of the thread directly at the structure method
-publicThread(Runnabletarget, Stringname)
4. At least two threads are started when the JAVA program starts
main: main method
gc: garbage collection
5. Thread sleep
If you do not use the methods provided in the Thread class, you can terminate the deferred operation through the method of an empty loop
publicstaticvoidsleep(ms) throwsInterruptedException
6, the forced operation of the thread
publicfinalvoidjoin() throwsInterrtupedException
#############################################)