线程创建的方式

方式1 继承 thread

public class TestThead {
    public static void main(String[] args) {
        Thread1 thread= new Thread1();
        thread.start();
    }
}

class Thread1 extends Thread{

    public void run() {
        System.out.println("thread1 继承 Thread");
    }
}
//thread1 继承 Thread
//缺点 类是单一继承的,不利于扩展

方式2 实现Runable

public class TestThead {
    public static void main(String[] args) {
        //写法一
        Thread thread = new Thread(new MyRunable());
        thread.start();
		//写法二
        Thread thread1 = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("匿名内部类写法.");
            }
        });
        thread1.start();
		//写法三
        Thread thread2 = new Thread(() -> System.out.println("新特性写法"));
        thread2.start();

    }
}

class MyRunable implements Runnable {

    @Override
    public void run() {
        System.out.println("MyRunable 实现 Runnable");
    }
}
// 实现 runable ,本身 Thread 也是实现了 Runnable
// 为什么都得用thread启动,因为实现类只有run方法,只有thread有start方法
// run() 和 start()的区别?
public class TestThead {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        Thread1 thread1 = new Thread1();
        thread1.run();
        System.out.println(Thread.currentThread().getName());
        System.out.println("===============================");
        Thread1 thread2 = new Thread1();
        thread2.start();
        // main
        // main
        // ===============================
        // Thread-1
    }
}
class Thread1 extends Thread {
    public void run() {
        System.out.println(Thread.currentThread().getName());
    }
}
// 直接run是虚拟机执行的  start是重新创建线程后主动调用run方法
// run必须是public 永远最高权限访问所有方法

方式3 实现Cellable

public class TestThead {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        FutureTask<String> futureTask = new FutureTask<String>(new MyCallable());
        Thread thread = new Thread(futureTask);
        thread.start();
        System.out.println(futureTask.get());
    }
}
class MyCallable implements Callable {

    @Override
    public Object call() throws Exception {
        return "mycellable  实现 Callable";
    }
}
//实现 callable接口  采用futureTask + thread
//可以携带返回值  允许抛异常

方式4 线程池创建

public class TestThead {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ExecutorService executorService = Executors.newFixedThreadPool(1);
        executorService.execute(new MyRunable());
        executorService.submit(new MyRunable());
    }
}


class MyRunable implements Runnable {

    @Override
    public void run() {
        System.out.println("MyRunable 实现 Runnable");
    }
}

//通过线程池来创建线程,执行任务,只能是实现runable的类
//缺点 线程池是工作队列是 无限的,当大量任务堆积到工作队列 就可能产生oom(内存溢出问题),不建议直接用Executors 创建线程池来创建线程
//execute 和 submit的区别
public class TestThead {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ExecutorService executorService = Executors.newFixedThreadPool(1);
        //executorService.execute(new MyCallable());//只能接受 myRunable
        executorService.execute(new MyRunable());
        Future future = executorService.submit(new MyCallable());
        System.out.println(future.get());
        //支持抛异常
        executorService.submit(()->{
            System.out.println("start...");
            int i = 10/0;
            System.out.println("...end");
        });
        //不支持抛异常
        executorService.execute(()->{
            System.out.println("start...");
            int i = 10/0;
            System.out.println("...end");
        });
    }
}

在这里插入图片描述
execute 和 submit的区别总结:
execute 只能实现runable submit 可以是runable 和 callable
execute 不能捕获异常 submit 可以捕获异常
AbstractExecutorService

 public Future<?> submit(Runnable task) {
        if (task == null) throw new NullPointerException();
        RunnableFuture<Void> ftask = newTaskFor(task, null);
        execute(ftask);
        return ftask;
    }

 public <T> Future<T> submit(Runnable task, T result) {
        if (task == null) throw new NullPointerException();
        RunnableFuture<T> ftask = newTaskFor(task, result);
        execute(ftask);
        return ftask;
    }
  public <T> Future<T> submit(Callable<T> task) {
        if (task == null) throw new NullPointerException();
        RunnableFuture<T> ftask = newTaskFor(task);
        execute(ftask);
        return ftask;
    }

ubmit 本质也是 execute
使用场景
如果不需要返回值 优先使用 execute
如果需要返回值 优先使用 submit

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值