package myMultithreadingDemo;
public class MyThread extends Thread{
public MyThread() {
}
public MyThread(String name) {
super(name);
}
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println(getName() + ": " + i);
}
}
}
package myMultithreadingDemo;
public class MyThreadDemo {
public static void main(String[] args) {
// MyThread mt1 = new MyThread();
// MyThread mt2 = new MyThread();
//public final synchronized void setName(String name); 将此线程的名称更改为参数name
// mt1.setName("小明");
// mt2.setName("小林");
//Thread(String name);
MyThread mt1 = new MyThread("小明");
MyThread mt2 = new MyThread("小林");
//直接调用run方法,不会开启线程
// mt1.run();
// mt2.run();
//public synchronized void start(); 导致此线程开始执行,Java虚拟机调用此线程的run方法
mt1.start();
mt2.start();
//public static native Thread currentThread(); 返回当前执行线程对象的引用
System.out.println(Thread.currentThread().getName());
}
}