当前位置: 首页> 科技> 互联网 > Java中线程的创建方式

Java中线程的创建方式

时间:2025/7/10 8:24:05来源:https://blog.csdn.net/qh1112/article/details/139756166 浏览次数:2次

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

public class MyThread{public static void main(String[] args) {Thread threadDome = new ThreadDome();threadDome.start();}
}class ThreadDome extends Thread{@Overridepublic 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{@Overridepublic 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>{@Overridepublic Integer call() throws Exception {return 10;}
}

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

四、使用线程池

1、线程池的实现:

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

    @Testpublic 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>() {@Overridepublic Integer call() throws Exception {return 10;}});System.out.println(submit.get());}//关闭线程池pool.shutdown();}

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

    @Testpublic 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>() {@Overridepublic Integer call() throws Exception {return 10;}}, 1, TimeUnit.SECONDS);System.out.println(schedule.get());}//销毁线程池pool.shutdown();}

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

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

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

@Testpublic void test04() throws ExecutionException, InterruptedException {//创建线程池ExecutorService pool = Executors.newSingleThreadExecutor();//使用线程池for (int i = 0; i < 5; i++) {Future<Integer> submit = pool.submit(new Callable<Integer>() {@Overridepublic 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()提高性能

关键字:Java中线程的创建方式

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: