public static void main(String[] args) {
//一:多线程,默认线程名称
MyThread my1 = new MyThread();
MyThread my2 = new MyThread();
my1.start();
my2.start();
//二:多线程,setName线程名称
MyThread my11 = new MyThread();
MyThread my22 = new MyThread();
my11.setName("高铁");
my22.setName("飞机");
my11.start();
my22.start();
//三:多线程:实现Thread 有参无参构造方法
MyThread my111 = new MyThread("高铁");
MyThread my222 = new MyThread("飞机");
my111.start();
my222.start();
}
public class MyThread extends Thread {
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println(getName()+":"+i);
}
}
public MyThread() {
}
public MyThread(String name) {
super(name);
}
二:实现Runnable接口 ( 推荐 )
public static void main(String[] args) {
//1.创建Runnable类的对象
MyRunnable myRunnable = new MyRunnable();
//2.创建Thread类对象,把MyRunnabl对象作为构造方法的参数
//Thread th1 = new Thread(myRunnable);
//Thread th2 = new Thread(myRunnable);
Thread th1 = new Thread(myRunnable, "高铁");
Thread th2 = new Thread(myRunnable, "飞机");
//3.启动线程
th1.start();
th2.start();
}
public class MyRunnable implements Runnable {
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println(Thread.currentThread().getName() + ":" + i);
}
}
}