Java Thread (i)
1. Process
Process: A program that is in progress.
Thread: A control unit (execution path) in the process that is responsible for program execution
Multiple paths can be executed in a thread, called multithreading.
A. There is at least one thread in a process.
B. Multiple threads are turned on to run multi-part code at the same time.
C. Each thread has its own running content. This content can be called a task that the thread will perform.
D. Multithreading benefits: Troubleshooting Multi-part operations
E. Disadvantages: Open more cards.
In fact, the implementation of the application is the CPU is doing a quick switch to complete, this switch is also random.
The JVM starts a thread and has at least two threads.
A. The thread that executes the main function.
The thread's task code is defined in the main function.
B. The thread responsible for garbage collection
2. Thread code
A. How do I create a thread?
Create a threading method one:
1. Inherit the thread class.
2. Overwrite the Run method in the thread class (defined in the Run method is the task code that the thread will run).
3. Create a thread for the subclass object that directly creates the thread class.
4. Call the Start method to open the thread and invoke the thread's task to execute the Run method.
Code:
Class Dome extends Thread {
private String name;
Dome (String name) {
THIS.name = name;
}
public void Run () {
System.out.println ("name=" + name);
}
}
Class threaddome2{
public static void Main (string[] args) {
Dome D1 = new Dome ("Xiaohei");
Dome D2 = new Dome ("Xiaoming");
D1.start ();
D2.start ();
}
}
Java thread (i)