并发编程(十)

带有Scheduled的基本都是定时器

Executor框架的成员

(1)ThreadPoolExecutor

ThreadPoolExecutor通常使用工厂类Executors来创建。Executors可以创建3种类型的ThreadPoolExecutor:SingleThreadExecutor、FixedThreadPool和CachedThreadPool。

下面分别介绍这3种ThreadPoolExecutor。

1)FixedThreadPool。下面是Executors提供的,创建使用固定线程数的FixedThreadPool的API。

FixedThreadPool适用于为了满足资源管理的需求,而需要限制当前线程数量的应用场景,它适用于负载比较重的服务器。

2)SingleThreadExecutor。下面是Executors提供的,创建使用单个线程的SingleThread-Executor的API。

SingleThreadExecutor适用于需要保证顺序地执行各个任务;并且在任意时间点,不会有多个线程是活动的应用场景。

3)CachedThreadPool。下面是Executors提供的,创建一个会根据需要创建新线程的CachedThreadPool的API。

CachedThreadPool是大小无界的线程池,适用于执行很多的短期异步任务的小程序,或者是负载较轻的服务器。

(2)ScheduledThreadPoolExecutor

ScheduledThreadPoolExecutor通常使用工厂类Executors来创建。Executors可以创建2种类型的ScheduledThreadPoolExecutor,如下。

·ScheduledThreadPoolExecutor。包含若干个线程的ScheduledThreadPoolExecutor。

·SingleThreadScheduledExecutor。只包含一个线程的ScheduledThreadPoolExecutor。

下面分别介绍这两种ScheduledThreadPoolExecutor。

下面是工厂类Executors提供的,创建固定个数线程的ScheduledThreadPoolExecutor的API。

public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize)

public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize,ThreadFactory threadFactory)

ScheduledThreadPoolExecutor适用于需要多个后台线程执行周期任务,同时为了满足资源管理的需求而需要限制后台线程的数量的应用场景。下面是Executors提供的,创建单个线程的SingleThreadScheduledExecutor的API。

SingleThreadScheduledExecutor适用于需要单个后台线程执行周期任务,同时需要保证顺序地执行各个任务的应用场景。

(3)Future接口

Future接口和实现Future接口的FutureTask类用来表示异步计算的结果。当我们把Runnable接口或Callable接口的实现类提交(submit)给ThreadPoolExecutor或ScheduledThreadPoolExecutor时,ThreadPoolExecutor或ScheduledThreadPoolExecutor会向我们返回一个FutureTask对象。下面是对应的API。

(4)Runnable接口和Callable接口

Runnable接口和Callable接口的实现类,都可以被ThreadPoolExecutor或ScheduledThreadPoolExecutor执行。它们之间的区别是Runnable不会返回结果,而Callable可以返回结果。

Runnable接口和Callable接口都是Java中用于实现多线程的接口,但它们有一些不同之处。

Runnable接口:

Runnable接口是一个简单的接口,只包含一个run()方法,用于定义线程执行的代码。当一个类实现了Runnable接口后,可以将该类的对象作为参数传递给Thread类的构造函数,从而创建一个新的线程对象。在run()方法中编写线程执行的代码,当调用线程对象的start()方法时,线程开始执行run()方法中的代码。

实现Runnable接口的步骤如下:

创建一个类并实现Runnable接口。

在实现类中重写run()方法,编写线程执行的代码。

创建一个Thread对象,并将实现Runnable接口的对象作为参数传递给Thread构造函数。

调用Thread对象的start()方法启动线程。

例如:

public class MyRunnable implements Runnable {  
    public void run() {  
        // 线程执行的代码  
    }  
}  
  
Thread thread = new Thread(new MyRunnable());  
thread.start();

Callable接口:

Callable接口与Runnable接口类似,也是一个简单的接口,包含一个call()方法,用于定义线程执行的代码。与Runnable不同的是,Callable可以返回结果,而Runnable没有返回值。当一个类实现了Callable接口后,可以将该类的对象传递给Executor框架中的相关方法,例如FutureTask或线程池的submit()方法,从而异步执行任务并返回结果。

实现Callable接口的步骤如下:

创建一个类并实现Callable接口。

在实现类中重写call()方法,编写线程执行的代码,并返回结果。

将实现Callable接口的对象传递给Executor框架的相关方法(例如FutureTask或线程池的submit()方法)。

通过Future对象获取任务执行的结果。

例如:

public class MyCallable implements Callable<Integer> {  
    public Integer call() throws Exception {  
        // 线程执行的代码,并返回结果  
        return res;  
    }  
}  
  
ExecutorService executor = Executors.newFixedThreadPool(1);  
Future<Integer> future = executor.submit(new MyCallable());  
Integer result = future.get(); // 获取任务执行的结果

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值