如果一个类直接继承了Thread类将会造成单继承的局限
Runnable接口
public interface Runnable{
public void run();
}
}
Thread 构造方法
public Thread(Runnable target){}
多线程的启动永远都是Thread类的start()方法
对Runnable接口对象可以采用匿名对象,匿名内部类和Lambda函数表达式来定义
匿名内部类:
public class Test {
public static void main(String args[]) {
new Thread(new Runnable(){
public void run() {
System.out.println("Hello world");
}
}).start();
}
}
使用Lambda函数表达式:
public class Test {
public static void main(String args[]) {
new Thread(()->System.out.println("Hello world")).start();
}
}