JAVA EE 多线程 --- 创建线程的5大方法!

方法一 : 继承Thread对run重写

1.MyThread 继承了Thread 然后对run进行重写

class MyThread extends Thread{
    @Override
    public void run() {
        while (true){
            System.out.println("hello world!");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

2.对MyThread进行实例化,调用start()启动线程!

public class ThreadDemo1 {
    public static void main(String[] args) throws InterruptedException {
        MyThread myThread = new MyThread();
        myThread.start();
        while (true){
            System.out.println("main!");
            Thread.sleep(1000);
        }
    }
}

方法二 : 实现Runnable接口,重写run

1.实现Runnable接口,重写run,其中Runnable仅代表一段代码,具体线程还是需要搭配Thread类,其的好处是降低了代码的耦合性!

class  MyThread1 implements Runnable {
    @Override
    public void run() {
        while (true){
            System.out.println("hello");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

2.创建 Thread 类实例, 调⽤ Thread 的构造⽅法时将 Runnable 对象作为 target 参数,调用start()启动线程!

public class ThreadDemo2 {
    public static void main(String[] args) throws InterruptedException {
        Runnable runnable = new MyThread1();
        Thread thread = new Thread(runnable);
        thread.start();
        while (true){
            System.out.println("mian");
            Thread.sleep(1000);
        }
    }
}

方法三 : 继承Thread对run重写(使用匿名内部类)

写{}是定义一个类,与此同时并继承自Thread,在此次定义子类的属性与方法,主要目的在于重写run

public class ThreadDemo3 {
    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(){
            @Override
            public void run() {
                while (true){
                    System.out.println("hello!");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };
        thread.start();
        while (true){
            System.out.println("main");
            Thread.sleep(1000);
        }
    }
}

方法四 : 实现Runnable接口,重写run(使用匿名内部类)

Thread构造方法的参数,填写了Runnable的匿名内部类的实例

public class ThreadDemo4 {
    public static void main(String[] args) throws InterruptedException {
        Thread t = new Thread(new Runnable(){
            @Override
            public void run() {
                while(true){
                    System.out.println("hello");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        t.start();
        while (true){
            System.out.println("mian");
            Thread.sleep(1000);
        }
    }
}

方法五 : [常用/推荐]使用lambda表达式

lambda 表达式创建 Runnable ⼦类对象(lambda代替了Runnable的位置)
public class ThreadDemo5 {
    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(()->{
           while (true){
               System.out.println("hello!");
               try {
                   Thread.sleep(1000);
               } catch (InterruptedException e) {
                   e.printStackTrace();
               }
           }
        });
        thread.start();
        while (true){
            System.out.println("main");
            Thread.sleep(1000);
        }
    }
}

  • 7
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

追鸣--守仰

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值