Java thread learning, java thread
I. Thread Creation
There are two ways to create a Thread in java: Inherit the Thread class and implement the Runnable interface.
1. inherit the Thread class
The Thread class is defined in Java. you can create your own Thread class by inheriting the Thread class and overwriting its run () method. The Code executed by the Thread is included in the run method.
Public ClassName extends Thread {
Public void run (){
}
}
2. Implement the Runnable interface
Another thread implementation method is to implement the Runnable interface. If the custom thread needs to inherit other classes, the first method cannot be used to create them. The java language does not support multi-inheritance, but multiple interfaces can be implemented, the thread must also implement the run method in the Runnable interface. The code to be executed by the thread is included in the run method.
Class MyThread implements Runnable {
Public void run (){
}
}
Ii. Thread startup
It is very easy to start a Thread that inherits the Thread class method. You only need to create a Thread class instance and call its start () method to start the Thread.
ClassName c = new ClassName ();
C. start ();
The Thread created by implementing the Runnable interface is first converted to the Thread class, and then the Thread starts by calling the start () method of the Thread class.
MyThread mt = new MyThread ();
Thread t = new Thread (mt );
T. start ();
3. Thread Lifecycle
Threads in a life cycle mainly include creation, readiness, running, blocking, and death.
1. Creation: After the Thread class is instantiated using the new keyword, the thread is in the Creation state before the start () method is called. A thread in the creation state only allocates memory space, which is the initial state of the lifecycle.
2. Ready: The thread is ready after the start () method is called. A ready thread has all the resources required to run except the CPU. The ready state thread waits in queue for the CPU, which is allocated by the system scheduling.
3. Run: After a ready thread obtains the CPU, it is in the running state. The running thread starts the content of the run () method.
4. Blocking: If a running thread cannot continue execution for some reason, it will enter the blocking status. The blocking status is different from the ready status: the ready status is not executed because of the lack of CPU, and the blocking status is because the thread cannot be executed due to various reasons, not just the lack of CPU. After the cause of blocking is removed, the thread is converted to the ready state again.
5. Death: When the thread executes the content of the run () method or is forcibly terminated, the thread is in the dead state and the entire lifecycle ends.
4. Thread Scheduling
1. thread priority: in java, the priority is set for the thread by calling the setPriority () method. The priority is 1 ~ A value of 10 indicates that a greater number indicates a higher priority. For example, c. setPriority (1), and then start the thread.
2. thread sleep: For a running thread, you can call the sleep () method to discard the CPU for sleep. This thread turns into a blocking state. The sleep () method contains long parameters, used to specify the thread sleep time, in milliseconds.
3. Thread concession: For the thread being executed, you can call the yield method to re-queue the thread and send the CPU to the later thread. The thread is ready. The yield () method only gives in to threads with a higher or equal priority.
4. Thread wait: For the thread being executed, you can call the join () method to wait until the end of the thread and then execute other programs.
Example: c. start ();
C. join ();
T. start;
Wait until the c thread finishes executing t.
5. Thread Synchronization
When multiple threads operate on the same shared resource, such as reading and writing the same variable, there is a resource competition problem. To solve this problem, a synchronization mechanism is required. In java, the synchronized keyword is used to synchronize threads.
Example:
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 result is as follows:
0
0
0
0
0
0
1
0
0
1
0
1
The adjustment is 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 output result is 0.