Lambda语法:
(参数列表) -> {业务代码实现…}
多线程的创建一共有三种方法:
1.继承Thread重写run方法
2.实现Runnable接口
3.实现Callable接口
一、继承 Thread进行实现
public class ThreadDemo3 {
public static void main(String[] args) {
//获得当前线程
Thread mainThread = Thread.currentThread();
System.out.println("线程名称:" + mainThread.getName());
Thread thread = new MyThread();
//开启线程
//执行start方法,才会创建一个新线程,且只能调用一次
thread.start();
//执行run方法,只是使用main线程调用一个普通方法而已,普通方法可以重复调用
// thread.run();
}
}
//新创建的自己的线程,新线程继承了主线程
class MyThread extends Thread{
@Override
public void run() {
//具体的业务执行代码
Thread thread = Thread.currentThread();
System.out.println("线程名称:" + thread.getName());
}
}
此方式的缺点:
Java 是单继承,继承了 Thread 就不能继承其他类了。
二、实现Runnable接口(以及变种方法)
实现Runnable接口新建线程
public class ThreadDemo4 {
public static void main(String[] args) {
//创建Runnable
MyThread2 myThread2 = new MyThread2();
//创建一个线程
Thread thread = new Thread(new MyThread2());
thread.start();
}
}
class MyThread2 implements Runnable{
@Override
public void run() {
//具体业务代码
Thread thread = Thread.currentThread();//得到当前线程
System.out.println("线程名称:" + thread.getName());
}
}
变种⽅法 1:匿名 Runnable ⽅式
public static void main(String[] args) {
//匿名内部类
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
//业务代码
Thread thread = Thread.currentThread();
System.out.println("线程名称:" + thread.getName());
}
});
//启动线程
thread.start();
}
变种⽅法 2:匿名⽅式创建⼦对象
public static void main(String[] args) {
//创建线程并初始化
Thread thread = new Thread(){
@Override
public void run() {
Thread t = Thread.currentThread();
System.out.println("线程名称:" + t.getName());
}
};
thread.start();
}
变种⽅法 3:使⽤ Lambda 匿名 Runnable ⽅式
(JDK 1.8+ 版本,推荐使用此方法)
public static void main(String[] args) {
Thread thread = new Thread(() -> {
//具体业务
Thread t = Thread.currentThread();
System.out.println("线程名称:" + t.getName());
});
thread.start();
}
以上创建方式有一个共同的问题:没有返回值。
也就是当线程执行完成之后,主线程是没有办法拿到新线程的执行结果。
三、带返回值的 Callable
import java.util.Random;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
/**
* 实现Callable新建线程
*/
public class ThreadDemo8 {
public static void main(String[] args) throws ExecutionException, InterruptedException {
//创建Callable实例
MyCallable myCallable = new MyCallable();
//用于接收Callable结果的对象
FutureTask<Integer> futureTask = new FutureTask<>(myCallable);
//创建新线程
Thread thread = new Thread(futureTask);
thread.start();
//接收新线程执行的结果
int result = futureTask.get();
System.out.println(Thread.currentThread().getName() + "--新线程返回的结果为:" + result);
}
}
//Callable<V> 泛型里面可以是任意数据类型
class MyCallable implements Callable<Integer>{
@Override
public Integer call() throws Exception {
//生成随机数0-9
int randomNum = new Random().nextInt(10);
System.out.println(Thread.currentThread().getName() +
"--随机数:" + randomNum);
return randomNum;
}
}
匿名 Callable
public static void main(String[] args) throws ExecutionException, InterruptedException {
FutureTask<String> futureTask = new FutureTask<>(new Callable<String>() {
@Override
public String call() throws Exception {
//新线程执行的业务代码
String[] arrs = new String[]{"Java","MySQL","Thread"};
//随机返回一个字符串
String result = arrs[new Random().nextInt(3)];
System.out.println(Thread.currentThread().getName() +
"--字符串:" + result);
return result;
}
});
//创建新线程
Thread thread = new Thread(futureTask);
thread.start();
String result = futureTask.get();
System.out.println(Thread.currentThread().getName() +
"--新线程的返回值" + result);
}