Java中线程的创建方式

一、继承Thread类,重写run方法

public class MyThread{
    public static void main(String[] args) {
        Thread threadDome = new ThreadDome();
        threadDome.start();
    }
}

class ThreadDome extends Thread{
    @Override
    public void run() {
        for (int i = 0; i < 5; i++) {
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println("i = " + i);
        }
    }
}

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

public class MyThread{   
    public static void main(String[] args) {
        MyRunnable myRunnable = new MyRunnable();
        new Thread(myRunnable).run();
    }
}

class MyRunnable implements Runnable{
    @Override
    public void run() {
        for (int i = 0; i < 5; i++) {
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println("i = " + i);
        }
    }
}

三、实现Callable接口,重写call方法

public class MyThread{
    public static void main(String[] args) throws ExecutionException, InterruptedException             
     {
        MyCallable myCallable = new MyCallable();
        FutureTask futureTask = new FutureTask(myCallable);
        new Thread(futureTask).start();

        System.out.println(futureTask.get());
    }
}

class MyCallable implements Callable<Integer>{
    @Override
    public Integer call() throws Exception {
        return 10;
    }
}

callable相较于runnable接口,可以拥有返回值,并且runnable中程序出现异常只能通过try...catch进行捕获然后处理,callable可以将异常抛出。

四、使用线程池

1、线程池的实现:

1、FixedThreadPool(定长线程池):只有核心线程,线程数量固定,执行完立即回收,任务队列为链表结果的有界队列,控制线程的最大并发数

    @Test
    public void test() throws ExecutionException, InterruptedException {
        //创建线程池
        ExecutorService pool = Executors.newFixedThreadPool(5);
        //使用线程池
        for (int i = 0; i < 5; i++) {
            Future<Integer> submit = pool.submit(new Callable<Integer>() {
                @Override
                public Integer call() throws Exception {
                    return 10;
                }
            });
            System.out.println(submit.get());
        }
        //关闭线程池
        pool.shutdown();
    }

2、ScheduledThreadPool(定时线程池):核心线程数量固定,非核心线程数量无限,执行完闲置10s后回收,任务队列为延时阻塞队列,执行定时或周期性任务

    @Test
    public void test02() throws ExecutionException, InterruptedException {
        //创建线程池
        ScheduledExecutorService pool = Executors.newScheduledThreadPool(5);
        //使用线程池
        for (int i = 0; i < 5; i++) {
            ScheduledFuture<Integer> schedule = pool.schedule(new Callable<Integer>() {
                @Override
                public Integer call() throws Exception {
                    return 10;
                }
            }, 1, TimeUnit.SECONDS);
            System.out.println(schedule.get());
        }
        //销毁线程池
        pool.shutdown();
    }

3、CachedThreadPool(可缓存线程池):无核心线程,非核心线程数量无限,执行完闲置60s回收,任务队列为不存储元素的阻塞队列,执行大量,耗时少的任务

    @Test
    public void test03() throws ExecutionException, InterruptedException {
        //创建线程池
        ExecutorService pool = Executors.newCachedThreadPool();
        //使用线程池
        for (int i = 0; i < 5; i++) {
            Future<Integer> submit = pool.submit(new Callable<Integer>() {
                @Override
                public Integer call() throws Exception {
                    return 10;
                }
            });
            System.out.println(submit.get());
        }
        //关闭线程池
        pool.shutdown();
    }

4、SingleThreadExecutor(单线程化线程池):只有一个核心线程,无非核心线程,执行完立即回收,任务队列为链表结果的有界队列,不适合做并发但可能引起io阻塞及影响ui线程响应的操作,如数据库操作、文件操作等

@Test
    public void test04() throws ExecutionException, InterruptedException {
        //创建线程池
        ExecutorService pool = Executors.newSingleThreadExecutor();
        //使用线程池
        for (int i = 0; i < 5; i++) {
            Future<Integer> submit = pool.submit(new Callable<Integer>() {
                @Override
                public Integer call() throws Exception {
                    return 10;
                }
            });
            System.out.println(submit.get());
        }
        //关闭线程池
        pool.shutdown();
    }

2、excute和submit的区别

excute:只能提交Runable类型任务,会直接抛出异常,可以用try、catch来捕获,和普通线程处理方式一致,excute没有返回值

submit:能提交Runable类型任务也能提交Callable类型任务,会吃掉异常,可通过future的get方法将任务执行时的异常重新抛出,submit有返回值,需要返回值的时候必须使用submit

submit最后的任务也是抛给excute方法,提交任务不需要一个结果的话,直接用excute()提高性能

  • 10
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值