Java中Executor接口的说明和使用

 

         Executor接口位于java.util.concurrent包中,属于public类型的接口。可以用于提交,管理或者执行Runnable任务。实现Executor接口的class还可以控制Runnable任务执行线程的具体细节。一般来说,Runnable任务开辟在新线程中的使用方法为:

 new Thread(new(RunnableTask())).start() 

        但是在Executor中可以用以下方式使用:

 Executor executor = anExecutor;
 executor.execute(new RunnableTask1());
 executor.execute(new RunnableTask2());
 ...

 Executor并没有严格规定执行Runnable任务的同步/异步状态,以及线程的使用方式,所以一切都相当的自由。你可以在调用线程中直接运行任务,例如:

 class DirectExecutor implements Executor {
     public void execute(Runnable r) {
         r.run();
     }
 }

 或者可以再另一个线程中启动:

 class ThreadPerTaskExecutor implements Executor {
     public void execute(Runnable r) {
         new Thread(r).start();
     }
 }

 也可以在实现中用另一个Executor来序列化执行过程:

 class SerialExecutor implements Executor {
     final Queue<Runnable> tasks = new ArrayDeque<Runnable>();
     final Executor executor;
     Runnable active;

     SerialExecutor(Executor executor) {
         this.executor = executor;
     }

     public synchronized void execute(final Runnable r) {
         tasks.offer(new Runnable() {
             public void run() {
                 try {
                     r.run();
                 } finally {
                     scheduleNext();
                 }
             }
         });
         if (active == null) {
             scheduleNext();
         }
     }

     protected synchronized void scheduleNext() {
         if ((active = tasks.poll()) != null) {
             executor.execute(active);
         }
     }
 }


另外Executors类提供了工厂方法,ExecutorService提供更多扩展性接口,ThreadPoolExecutor提供线程池支持。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在GlassFishThread Pool被用于管理应用程序的并发请求。以下是在GlassFish使用Thread Pool的一个简单示例: 1. 首先,你需要在GlassFish创建一个Thread Pool。你可以使用GlassFish控制台或命令行工具来完成这个任务。例如,使用控制台,你可以依次点击“Configurations”、“server-config”、“Thread Pools”、“New”来创建一个新的Thread Pool。 2. 然后,你需要在你的应用程序使用这个Thread Pool。你可以使用Java EE的@ManagedExecutorService注解来注入一个Thread Pool,或者使用JavaExecutorService接口来手动创建一个Thread Pool。例如,下面是使用@ManagedExecutorService注解的示例: ``` import javax.annotation.Resource; import javax.enterprise.concurrent.ManagedExecutorService; import javax.enterprise.context.ApplicationScoped; @ApplicationScoped public class MyService { @Resource private ManagedExecutorService executor; public void doSomething() { executor.submit(() -> { // 处理请求的代码 }); } } ``` 在这个示例,我们注入了一个ManagedExecutorService对象,并将其用于处理请求。当我们调用doSomething()方法时,我们使用线程池执行一个Lambda表达式,该表达式包含了我们要处理的请求。 3. 最后,你需要配置Thread Pool的属性。你可以在GlassFish控制台编辑Thread Pool的属性,或者在应用程序的persistence.xml文件使用Java EE的concurrency元素来配置Thread Pool的属性。例如,下面是在persistence.xml文件配置Thread Pool的示例: ``` <?xml version="1.0" encoding="UTF-8"?> <persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"> <persistence-unit name="my-pu"> <jta-data-source>jdbc/mydb</jta-data-source> <properties> <property name="javax.persistence.concurrent.max-thread-pool-size" value="10"/> <property name="javax.persistence.concurrent.timeout" value="5000"/> </properties> </persistence-unit> </persistence> ``` 在这个示例,我们在persistence.xml文件配置了Thread Pool的最大线程数和超时时间。这些属性将被用于管理我们的并发请求。 总之,使用Thread Pool可以在GlassFish提高应用程序的性能和可伸缩性。你可以使用Java EE的@ManagedExecutorService注解或JavaExecutorService接口使用Thread Pool,并使用GlassFish控制台或应用程序的persistence.xml文件来配置Thread Pool的属性。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值