线程创建方式

一、继承Thread类,重写run方法
public class MyThread1 extends Thread {
    @Override
    public void run() {
        String threadName = super.getName();
        for (int i = 0; i < 10; i++) {
            try {
                Thread.sleep(100);
                System.out.println(threadName + i);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    public MyThread1(String name) {
        super(name);
    }
    public static void main(String[] args) {
        MyThread1 myThread1 = new MyThread1("第一个线程");
        myThread1.start();
        MyThread1 myThread2 = new MyThread1("第二个线程");
        myThread2.start();
    }
}
二、实现Runnable接口,实现run方法
public class MyThread2 implements Runnable {
    public String name;

    public MyThread2(String name) {
        this.name = name;
    }

    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            try {
                Thread.sleep(100);
                System.out.println(name + i);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        Thread thread1 = new Thread(new MyThread2("第一个线程"));
        thread1.start();
        Thread thread2 = new Thread(new MyThread2("第二个线程"));
        thread2.start();
    }
}

实现Runnable接口相比第一种继承Thread类的方式,使用了面向接口,将任务与线程进行分离,有利于解耦

三、匿名内部类的方式
public class MyThread3 {
    public static void main(String[] args) {
        //方式1:Thread作为匿名内部类
        new Thread(){
            @Override
            public void run() {
                System.out.println("匿名类方式创建线程1");
            }
        }.start();

        //方式2:Runnable作为匿名内部类
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("匿名类方式创建线程2");
            }
        }).start();
    }
}
  • 上面的匿名内部类可以用Lambda(基于jdk1.8)表示
public class MyThread3 {
    public static void main(String[] args) {
        //方式1:Thread作为匿名内部类
        new Thread(() -> System.out.println("匿名类方式创建线程1")).start();

        //方式2:Runnable作为匿名内部类
        new Thread(() -> System.out.println("匿名类方式创建线程2")).start();
    }
}
四、实现Callable接口,实现call方法,call返回值为接口的泛型V
public class MyThread4 implements Callable<String> {
    @Override
    public String call() throws Exception {
        System.out.println("正在执行新建线程任务");
        Thread.sleep(2000);
        return "新建线程睡了2s后返回执行结果";
    }

    public static void main(String[] args) {
        try {
            FutureTask<String> task = new FutureTask<>(new MyThread4());
            Thread t = new Thread(task);
            t.start();
            System.out.println("提前完成任务...");
            String result = task.get();
            System.out.println(result);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
五、线程池创建线程
  • 统一管理线程,降低了创建线程和销毁线程的时间开销和资源浪费
public class MyThread5 {
    public static void main(String[] args) {
        ExecutorService executorService = Executors.newFixedThreadPool(5);
        for (int i = 0; i < 10; i++) {
            executorService.execute(new Runnable() {
                @Override
                public void run() {
                    System.out.println(Thread.currentThread().getName());
                }
            });
        }
        executorService.shutdown();
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值