线程创建方式
继承Thread:
//使用
MyThread myThread = new MyThread("myThread");
myThread.start();
//创建
public class MyThread extends Thread{
public MyThread() {
}
public MyThread(String name) {
super(name);
}
@Override
public void run() {
for (int i = 0; i < 200; i++) {
System.out.println(getName()+"正在执行:"+i);
}
}
}
实现Runnable:
MyRunable myRunable = new MyRunable();
Thread myRunnable = new Thread(myRunable, "myRunnable");
myRunnable.start();
public class MyRunable implements Runnable{
@Override
public void run() {
for (int i = 0; i < 200; i++) {
System.out.println(Thread.currentThread().getName()+"正在执行:"+i);
}
}
}
匿名内部类:
new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 200; i++) {
System.out.println(Thread.currentThread().getName()+"正在执行"+i);
}
}
},"myRunnable").start();