One, the creation of threads
There are two ways of creating threads in Java: Inheriting the thread class and implementing the Runnable interface.
1. Inheriting the thread class
Thread classes are defined in the Java language, and users can create their own thread classes by inheriting the thread class, overwriting their run () method, and the code that the thread executes is included in the Run method.
Public ClassName extends thread{
public void Run () {
}
}
2. Implement the Runnable interface
Another thread is implemented by implementing the Runnable interface. If the custom thread also inherits other classes, it cannot be created in the first way, the Java language does not support multiple inheritance, but it can implement multiple interfaces, the thread also implements the Run method in the Runnable interface, and the code that the thread needs to execute is included in the Run method.
Class MyThread implements runnable{
public void Run () {
}
}
Second, the start of the thread
Thread initiation that inherits the way of the thread class is very simple, as long as the thread class instance is created and its start () method is called to complete the thread's start.
ClassName C = new ClassName ();
C.start ();
The thread that implements the Runnable interface is first converted to the thread class and then called by the Start () method of the thread class to start the thread.
MyThread MT = new MyThread ();
Thread t = new Thread (MT);
T.start ();
Third, the life cycle of the thread
Threads in a lifecycle consist primarily of creation, readiness, operation, blocking, and death states.
1. Create: Thread class is in the created state before invoking the start () method after it is instantiated with the new keyword. A thread that is in the created state allocates only memory space, which is the initial state of the life cycle.
2. Ready: After the thread invokes the start () method, it is in the ready state. A thread in the ready state has all the resources required to run in addition to the CPU. The ready state thread waits for the CPU to be allocated by the system dispatch.
3. Run: The thread in the ready state is running after the CPU is acquired. The thread in the running state only starts the content of the thread run () method that is actually executing.
4. Blocking: A running thread enters a blocking state if it is unable to continue execution for any reason. The blocking state differs from the ready state in that the ready state cannot be executed because the CPU is missing, and that the blocking state is caused by a variety of reasons that the thread cannot execute, not just the CPU. After the cause of the blockage is lifted, the thread is again turned into a ready state.
5. Death: When the thread finishes executing the contents of the run () method or is forced to terminate, the thread is in a dead state, ending the entire life cycle.
Iv. Scheduling of threads
1. Thread priority: In the Java language, the thread is prioritized by calling the SetPriority () method, and the priority is represented by the number of 1~10, the higher the number, the greater the priority. such as: c.setpriority (1), and then start the thread.
2. Thread hibernation: For a thread that is executing, you can call the sleep () method to discard the CPU for hibernation, the thread becomes blocked, and the Sleep () method contains a long parameter that specifies the time, in milliseconds, that the thread sleeps.
3. Thread concession: For a thread that is executing, you can call the yield method to re-queue it, give the CPU to the thread that is behind it, and turn the thread into a ready state. The yield () method only yields to high-priority or equal-priority threads.
4. Thread waits: For a thread that is executing, you can call the join () method to wait for it to end before executing another program.
such as: C.start ();
C.join ();
T.start;
Wait for the C thread to complete before executing T.
Five, thread synchronization
When multiple threads operate on the same shared resource, such as reading and writing the same variable, there is a problem of resource competition. To solve such problems, you need to use the synchronization mechanism. In the Java language, thread synchronization is implemented using the Synchronized keyword.
Examples are as follows:
Class MyThread implements runnable{
private int con = 0;
@Override
public void Run () {
Test ();
}
private void Test () {
for (int i = 0;i<10;i++) {
con++;
Thread.yield ();
con--;
System.out.println (con);
}
}
}
public class Testthread {
public static void Main (string[] args) {
MyThread t = new MyThread ();
thread T1 = new Thread (t);
Thread t2= new Thread (t);
T1.start ();
T2.start ();
}
}
The results are as follows:
0
0
0
0
0
0
1
0
0
1
0
1
Adjust as follows:
Class MyThread implements runnable{
private int con = 0;
@Override
public void Run () {
Test ();
}
Private synchronized void Test () {
for (int i = 0;i<10;i++) {
con++;
Thread.yield ();
con--;
System.out.println (con);
}
}
}
public class Testthread {
public static void Main (string[] args) {
MyThread t = new MyThread ();
thread T1 = new Thread (t);
Thread t2= new Thread (t);
T1.start ();
T2.start ();
}
}
The results of the output are all 0.
Java Thread Learning