Method to realize
1. To achieve a thread through inheritance Thread class.
2. Implement a thread by implementing Runnable interface
3. Create a thread by thread pool.
Note:
Inheritance expansion is not strong, java total support single inheritance, if a class inherits Thread can not inherit other classes.
How to start?
Inheritance simple to start:
package com.cs.Thread_Test;
public class MyThread extends Thread{
@Override
public void run() {
super.run();
System.out.println("run方法执行了:OK");
}
}
class demoTest{
public static void main(String[] args) {
MyThread myThread = new MyThread();
//调用start()方法
myThread.start();
//地二种调用
//Thread thread1 = new Thread(new MyThread());
// thread1.start();
//第三种 实现Runnable接口
Thread thread = new Thread(new Runnable() {
public void run() {
System.out.println("runnable 执行了");
}
});
thread.start();
}
}
start method to start the thread () method, which is called after a successful start run method.
How to distinguish between a thread?
If I have a lot of threads in a system, each thread will print the log, how do I want to distinguish between the thread printed?
Just give the thread sets a thread name? setName ( ""); this is a norm, after the creation, to set the name