1、继承Thread类,重写run方法。
public class MyThread extends Thread {
public void run() {
System.out.println("this is my thread");
}
}
调用:
MyThread myThread = new MyThread();
myThread.start();
2、实现Runnable接口,实现run方法。
public class MyThread implements Runnable{
public void run() {
System.out.println("this is my Thread");
}
}
调用:
MyThread myThread = new MyThread();
Thread t = new Thread(myThread);
t.start();
网友评论