/***继承Thread类***/
public class MyThread extends Thread {
@Override
public void run() {
/***当前线程的名称***/
String name = Thread.currentThread().getName();
for (int i = 0; i < 20; i++) {
System.out.println("当前线程" + name + "---" + i);
}
}
public static void main(String[] args) {
MyThread thread1 = new MyThread();
MyThread thread2= new MyThread();
/***开启线程***/
thread2.start();
thread1.start();
}
}
/***实现runnable**/
public class MyThread2 implements Runnable {
@Override
public void run() {
/***当前线程的名称***/
String name = Thread.currentThread().getName();
for (int i = 0; i < 20; i++) {
System.out.println("当前线程" + name + "---" + i);
}
}
}