[Java基础]体验Lambda表达式
Posted Wecccccccc
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[Java基础]体验Lambda表达式相关的知识,希望对你有一定的参考价值。
普通写法:
代码如下:
package LambdaPack01;
public class MyRunnable implements Runnable{
@Override
public void run() {
System.out.println("多线程启动了");
}
}
package LambdaPack01;
public class LambdaDemo01 {
public static void main(String[] args) {
MyRunnable my = new MyRunnable();
Thread t = new Thread(my);
t.start();
}
}
匿名内部类:
代码如下:
package LambdaPack01;
public class LambdaDemo01 {
public static void main(String[] args) {
// MyRunnable my = new MyRunnable();
// Thread t = new Thread(my);
// t.start();
new Thread(new Runnable() {
@Override
public void run() {
System.out.println("多线程启动了");
}
}).start();
}
}
Lambda表达式:
代码如下:
package LambdaPack01;
public class LambdaDemo01 {
public static void main(String[] args) {
// MyRunnable my = new MyRunnable();
// Thread t = new Thread(my);
// t.start();
// new Thread(new Runnable() {
// @Override
// public void run() {
// System.out.println("多线程启动了");
// }
// }).start();
new Thread(()->{
System.out.println("多线程启动了");
}).start();
}
}
以上是关于[Java基础]体验Lambda表达式的主要内容,如果未能解决你的问题,请参考以下文章