1-----------------------------------Basic Concepts-------------------------------------------------
(1) Multithreading: One application has multiple execution paths
Process: The application being executed
Threads: Execution unit of a process, execution path
Single thread: One application has only one execution path
Multithreading: One application has multiple execution paths
What is the meaning of multi-process?
Improve CPU utilization
What is the meaning of multithreading?
Increase application usage
(2) Multi-threaded implementation scheme
A: Inherit the Thread class
B: Implement Runnable interface
(3) Scheduling of threads
A: Scheduling of threads
A: time-sharing scheduling
B: Preemptive scheduling (Java is using this scheduling method)
(4) Resolve thread safety issues synchronously
A: Synchronizing code blocks
Synchronized (object) {
Code that needs to be synchronized;
}
The lock object here can be any object.
B: Synchronization method
Add synchronization to the method.
The lock object here is this
C: Static Synchronization method
Add synchronization to the method.
The lock object here is the byte-code file object of the current class (reflection-re-bytecode file object)
(5) The life cycle of a thread
2-----------------------------------Base Code-------------------------------------------------
(1) Inherit thread
/**
* inherit thread
* @author Administrator
*
*/
public class XC1 Extends Thread {
String BB =null;
Public XC1 () {}
public XC1 (String aa) {
& nbsp; super ();
bb=aa;
}
@Override
public void Run () {
for (int i = 0; i < i++) {
&N Bsp SYSTEM.OUT.PRINTLN ("Thread 1:" +1111+BB);
}
}
}
(2) Implement Runnable interface
public class XC2 implements Runnable {
public void Run () {
for (int i = 0; i <; i++) {
SYSTEM.OUT.PRINTLN ("Thread 2:" +2222);
}
}
}
(3) Call
public class Demo1 {
public static void Main (string[] args) {
XC1 xc1 = new XC1 ();
Thread Th1 = new Thread (XC1);
Th1.start ();
XC2 Xc2 = new XC2 ();
Thread Th2 = new Thread (XC2);
Th2.start ();
}
}
Summary of Java Multi-threading