1 方法1:新建一个类继承Thread
public class FirstThreadTest {
public static void main(String[] args) {
MyThread mt = new MyThread();
mt.start();
for (int i = 1;i<=10; i++){
System.out.println(Thread.currentThread().getName() + "输出"+i);
}
}
}
class MyThread extends Thread{
@Override
public void run(){
for (int i = 1;i<=10; i++){
System.out.println(Thread.currentThread().getName() + "输出"+i);
}
}
}
2 方法2:实现Runnable接口
public class SecondThreadTest {
public static void main(String[] args) {
SecondThread st = new SecondThread();
//1 先创建一个Runnable实现类,再把这个类传给Thread,则可
//实现创建线程
Thread t = new Thread(st);
t.start();
for (int i = 1;i<=10; i++){
System.out.println(Thread.currentThread().getName() + "输出"+i);
}
}
}
class SecondThread implements Runnable{
@Override
public void run() {
for (int i = 1;i<=10; i++){
System.out.println(Thread.currentThread().getName() + "输出"+i);
}
}
}
----------------------------->>>>上述的输出为: