外部类实现线程接口:
public class TestLambda {
public static void main(String[] args) {
new Thread(new Race()).start();
}
}
class Race implements Runnable{
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + "运行中");
}
}
内部类实现线程接口:
public class TestLambda {
public static void main(String[] args) {
new Thread(new Race()).start();
}
//静态方法只能使用静态内部类
static class Race implements Runnable{
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + "运行中");
}
}
}
匿名内部类:
public class TestLambda {
public static void main(String[] args) {
new Thread(new Runnable(){
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + "运行中");
}
}).start();
}
}
jdk8的Lambda表达式:
public static void main(String[] args) {
new Thread(()->{
System.out.println(Thread.currentThread().getName() + "运行中");
}
).start();
}
网友评论