【Java EE初阶】创建线程的五种方式


1. 继承Tread,重写run()

示例:pandas 是基于NumPy 的一种工具,该工具是为了解决数据分析任务而创建的。

 class MyTread extends Thread {
    public void run() {
        while (true) {
            System.out.println("hello thread");

            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

    public class thread{
        public static void main(String[] args) {
            Thread t = new MyTread();
            t.start();
            while(true){
                System.out.println("hello world");
                try {
                    Thread.sleep(3000);
                }catch (InterruptedException e){
                    e.printStackTrace();
                }
            }
        }
    }

执行结果如下
在这里插入图片描述
如下图,有main,t两个线程
线程执行顺序是抢占式,开始之后,只有前两条结果是固定的,之后的顺序由操作系统控制,不可预测。
t.start();创建了一个新的线程,之后新的线程调用run()方法,打印“hello thread”。
run()方法执行完毕后,线程销毁。

2. 实现Runnable接口,重写run()

class MyRunnable implements Runnable{
    public void run(){
        while(true){
            System.out.println("hello runnable");
            try{
                Thread.sleep(2000);
            }catch(InterruptedException e){
                e.printStackTrace();
            }
        }
    }
}
public class UseRunnable{
    public static void main(String[] args) {
        Runnable r = new MyRunnable();
        Thread t = new Thread(r);
        t.start();
        while(true){
            System.out.println("hello main");
            try{
                Thread.sleep(2000);
            }catch(InterruptedException e){
                e.printStackTrace();
            }
        }
    }
}

执行结果如下
在这里插入图片描述
Thread t = new Thread(r),由线程t去完成r的任务,即执行run()方法。

3. 使用匿名内部类,继承Thread

public class InnerRunnable {
    public static void main(String[] args) {
        Thread t = new Thread(){
            public void run(){
                while(true){
                    System.out.println("hello thread");
                    try{
                        Thread.sleep(2000);
                    }catch(InterruptedException e){
                        e.printStackTrace();
                    }
                }
            }
        };
        t.start();
        while (true){
            System.out.println("hello main");
            try{
                Thread.sleep(2000);
            }catch(InterruptedException e){
                e.printStackTrace();
            }
        }
    }
}

4. 使用匿名内部类,实现Runnable

public class InnerRunnable {
    public static void main(String[] args) {
        Thread t = new Thread(new Runnable(){
            public void run(){
                while(true){
                    System.out.println("hello runnable");
                    try{
                        Thread.sleep(2000);
                    }catch(InterruptedException e){
                        e.printStackTrace();
                    }
                }
            }
        });
        t.start();
        while(true){
            System.out.println("hello main");
            try{
                Thread.sleep(2000);
            }catch(InterruptedException e){
                e.printStackTrace();
            }
        }
    }
}

在这里插入图片描述

5. 使用lambda表达式

lambda指的是匿名函数

public class Lambda {
    public static void main(String[] args) {
        Thread t = new Thread(() ->{
            while(true){
                System.out.println("hello lambda");
                try{
                    Thread.sleep(2000);
                }catch(InterruptedException e){
                    e.printStackTrace();
                }
            }
        });
        t.start();
        while(true){
            System.out.println("hello main");
            try{
                Thread.sleep(2000);
            }catch(InterruptedException e){
                e.printStackTrace();
            }
        }
    }
}

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值