ExecuteService学习

private static void executeRunnableTask() throws InterruptedException {
        ExecutorService executorService = Executors.newFixedThreadPool(2);
        IntStream.range(0, 10).boxed().forEach(i->executorService.execute(new MyTask(i) {
            
            @Override
            protected void error(Throwable e) {
                System.out.println(Thread.currentThread().getName()+" The no:"+i+ " failed, update status to fail");
            }
            
            @Override
            protected void done() {
                System.out.println(Thread.currentThread().getName()+ " The no:"+i+ " success, update status to success");
            }
            
            @Override
            protected void doInit() {
                // TODO Auto-generated method stub
                
            }
            
            @Override
            protected void doExecute() {
                if(i%3==0) {
                    int temp = i/0;
                }
            }
        }));
//        executorService.shutdown();
//        executorService.awaitTermination(10, TimeUnit.SECONDS);
        System.out.println("=============================");
    }
    private abstract static class MyTask implements Runnable{
        protected final int no;
        
        public MyTask(int no) {
            super();
            this.no = no;
        }

        @Override
        public void run() {
            try {
                this.doInit();
                this.doExecute();
                this.done();
            }catch (Throwable e) {
                this.error(e);
            }
        }

        protected abstract void error(Throwable e);

        protected abstract void done();

        protected abstract void doExecute();

        protected abstract void doInit();
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
策略模式是一种行为型设计模式,它允许在运行时动态地选择算法的行为,这些算法在同一接口中定义。在策略模式中,创建一个策略接口,然后定义多个实现该接口的具体策略类。接着,在使用策略的时候,需要根据不同的参数选择不同的策略类,然后调用其相应的方法。 在使用策略模式的时候,我们可以将不同的服务(service)接口封装成不同的策略类,在运行时根据参数选择不同的策略类,从而调用不同的服务接口。这可以使得代码更加灵活,易于扩展和维护。 下面是一个使用策略模式实现根据参数调用不同服务接口的示例代码: ```java // 策略接口 interface ServiceStrategy { void doService(); } // 具体策略类1 class Service1 implements ServiceStrategy { @Override public void doService() { System.out.println("调用服务1"); } } // 具体策略类2 class Service2 implements ServiceStrategy { @Override public void doService() { System.out.println("调用服务2"); } } // 上下文类 class ServiceContext { private ServiceStrategy strategy; public void setStrategy(ServiceStrategy strategy) { this.strategy = strategy; } public void executeService() { this.strategy.doService(); } } // 测试代码 public class Main { public static void main(String[] args) { ServiceContext context = new ServiceContext(); // 根据参数选择不同的策略类 String serviceType = "service1"; if (serviceType.equals("service1")) { context.setStrategy(new Service1()); } else if (serviceType.equals("service2")) { context.setStrategy(new Service2()); } // 调用服务 context.executeService(); } } ``` 在上面的示例代码中,我们定义了一个策略接口 `ServiceStrategy`,并实现了两个具体的策略类 `Service1` 和 `Service2`,分别对应不同的服务接口。然后,我们定义了一个上下文类 `ServiceContext`,用于执行服务。在上下文类中,我们使用了策略模式,根据参数选择不同的策略类,然后调用其相应的服务接口。最后,在测试代码中,我们演示了如何使用策略模式调用不同的服务接口。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值