目录
方法五:lambda表达式创建Runnable子类对象(推荐)
前言:
在充分的了解完线程的基本内容后,我们就可以去了解怎样创建一个多线程程序。
本文将从多个角度切入,用五个方法来介绍如何创建出一个多线程程序。在日常开发中,推荐使用第五个方法创建线程。
序列:多线程 - 002
方法一:继承 Thread 类
通过继承Thread标椎类,来创建一个线程类:
class MyThread extends Thread{//通过继承Thread类,来创建新的线程类MyThread
@Override
public void run() {//重写父类中的run()方法
while (true){
System.out.println("这里是线程的执行代码...");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
}
public class Method01 {
public static void main(String[] args) throws InterruptedException {//主线程
MyThread myThread = new MyThread();//创建线程类的对象myThread
myThread.start();//创建出myThread线程,并调用run()方法执行
while (true){
System.out.println("这里是main线程的执行代码...");
Thread.sleep(1000);
}
}
}
注意:
- start()方法和run()方法都是Thread类的成员方法;
- run()方法是线程的入口,start()方法则是调用系统的API,在系统中创建出新的线程,再让新线程调用run();
方法二:实现 Runnable 接口
实现 Runnable 接口,创建线程类:
class MyRunnable implements Runnable{//通过实现Runnable接口,来创建新的线程类
@Override
public void run() {
while (true){
System.out.println("这里是线程的执行代码...");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
}
public class Method02 {
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(new MyRunnable());//创建Thread类的实例,调用Thread类的构造方法,将MyRunnable类的对象作为参数传入,创建对象
thread.start();
while (true){
System.out.println("这里是main线程的执行代码...");
Thread.sleep(1000);
}
}
}
Thread thread = new Thread(new MyRunnable());
这里创建Thread类的实例,调用Thread类的构造方法,将MyRunnable类的对象作为参数传入,创建对象。
方法三:匿名内部类创建Thread子类对象
匿名内部类创建Thread子类对象:
public class Method03 {
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(){//匿名内部类
@Override
public void run() {
while (true){
System.out.println("这里是线程的执行代码...");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
};
thread.start();//创建线程thread,并调用run()方法
while (true){
System.out.println("这里是main线程的执行代码...");
Thread.sleep(1000);
}
}
}
注意:
- 匿名内部类,创建出来的新类是Thread类的子类,没有类名;
- 匿名内部类创建出来子类,可以重写父类方法,同时创建子类对象;
方法四:匿名内部类创建Runnable子类对象
匿名内部类创建Runnable子类对象:
public class Method04 {
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
while (true){
System.out.println("这里是线程的执行代码...");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
});
thread.start();
while (true){
System.out.println("这里是main线程的执行代码...");
Thread.sleep(1000);
}
}
}
方法五:lambda表达式创建Runnable子类对象(推荐)
lambda表达式创建Runnable子类对象:
public class Method05 {
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(()->{//创建线程,推荐使用lambda表达式
while (true){//lambda表达式,本身就是run()方法,不用重写run()方法
System.out.println("这里是线程的执行代码...");
try {
Thread.sleep(1000);//限定时间,便于观察
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
});
thread.start();//创建一个新线程,自动调用run()方法
while (true){
System.out.println("这里是main线程的执行代码...");
Thread.sleep(1000);//限定时间,便于观察
}
}
}
注意:
- 创建线程,推荐使用lambda表达式;
- lambda表达式,本身就是run()方法,不用重写run()方法;
- lambda表达式本质上是一个匿名函数,主要实现 “回调函数” 作
以上为Java中创建线程的五个方法。