This article is based on a 5-year hammer practice-the most basic Java video for beginners in history
Thread: A control unit (execution path) in the process that is responsible for program execution
Each thread has its own running content. This content can be called a task that the thread will perform.
Multithreading Benefits: Solves the problem of multiple parts running at the same time.
The disadvantage of multithreading: too many threads back to the reduction of efficiency.
When the JVM starts, it starts multiple threads, and at least two threads can parse it out.
1, the thread that executes the main function,
The thread's task code is defined in the main function.
2, the thread responsible for garbage collection.
How do I create a thread?
Create thread mode One: Inherit the Thread class.
Steps:
1, define a class that inherits the thread class.
2, overwrite the Run method in the thread class.
3, create a thread for the subclass object that directly creates the thread.
4, call the Start method to open the thread and invoke the thread's task to execute the Run method.
class Demo extends thread{ private String name; Demo (String name) { super (name); // this.name = name; run () { for (int x=0; x<10; X++ System.out.println (name+ ".... x=" +x+ "... name=" +thread.currentthread (). GetName ()); }}}
class ThreadDemo2 {
Public static void main (string[] args) {
Demo D1 = new Demo ("Wang Choi"); Demo D2 = new Demo ("Xiaoqiang"); D1.start ();//Open thread, call the Run method. D2.start (); System.out.println ("over ...." +Thread.CurrentThread (). GetName ()); }}
Java Learning-Multi-threading