The basic concept of a thread:
A thread is a sequential flow of control within a program, a process that is equivalent to a task, and a thread is equivalent to a single execution path in a task. Multi-process: multiple tasks (Programs) can be run simultaneously in the operating system; Multithreading: multiple sequential streams execute simultaneously in the same application; Java threads are implemented through the Java.lang.Thread class; a VM starts with a Main method (public static void The thread defined by main () {}) to create a new thread by creating an instance of thread; Each thread completes its operation by using the method run () of a particular thread object. Method Run () is called the thread body to start a thread by invoking the start () method of the Thread class
Creation and startup of two Java threads:
There are two ways to create a new thread:
First type:
1. Define thread class implement Runnable interface
2.Thread mythread = new Thread (target);//target is runnable interface type
There is only one method in 3.Runnable: public void Run (), which defines the thread-running body
4. Use the Runnable interface to provide shared data for multiple threads
5. The static method public static thread CurrentThread () of thread can be used in the definition of the run () method of the class that implements the Runnable interface (); Gets the reference of the current thread
The second type:
1. You can define a subclass of thread and override its run method such as:
class MyThread extends Thread {
public void run() {...}
}
2. Then generate the object of the class:
Mythread mythread = new Mythread ();
Three basic methods of Java Threading Control:
IsAlive (): Determine if the thread is still "alive"
GetPriority (): Gets the priority value of the thread
SetPriority (): Set priority values for threads
Thread.Sleep (): Specify the number of milliseconds for current thread sleep
Join (): Invokes the method of a thread, "merges" the current thread with the thread, waits for the thread to end, and then restores the current thread's running
Yield (): Let the CPU, the current thread into the ready queue waiting for scheduling
Wait (): The current thread enters the object's
Wait pool notify ()/notifyall (): Wakes one/all waiting threads in the await pool of the object