0
点赞
收藏
分享

微信扫一扫

java编写一个可执行指定接口类的所有实现,要求支持同步/异步、按指定序执行、支持分类执行

以下是一个基于Java语言的示例代码,用于执行指定接口类的所有实现,支持同步/异步执行、按指定序执行,并且支持分类执行。


import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;

public class InterfaceExecutor {

    // 定义接口类
    public interface MyInterface {
        void execute();
    }

    // 实现接口类的具体实现类
    public static class MyImplementation1 implements MyInterface {
        @Override
        public void execute() {
            System.out.println("MyImplementation1 executing");
        }
    }

    public static class MyImplementation2 implements MyInterface {
        @Override
        public void execute() {
            System.out.println("MyImplementation2 executing");
        }
    }

    public static class MyImplementation3 implements MyInterface {
        @Override
        public void execute() {
            System.out.println("MyImplementation3 executing");
        }
    }

    public static void main(String[] args) throws InterruptedException, ExecutionException {
        // 创建接口类实现列表
        List<MyInterface> implementations = new ArrayList<>();
        implementations.add(new MyImplementation1());
        implementations.add(new MyImplementation2());
        implementations.add(new MyImplementation3());

        // 同步执行
        for (MyInterface implementation : implementations) {
            implementation.execute();
        }

        // 异步执行
        ExecutorService executorService = Executors.newFixedThreadPool(implementations.size());
        List<Future<?>> futures = new ArrayList<>();

        for (final MyInterface implementation : implementations) {
            Future<?> future = executorService.submit(new Runnable() {
                @Override
                public void run() {
                    implementation.execute();
                }
            });
            futures.add(future);
        }

        // 等待所有异步任务完成
        for (Future<?> future : futures) {
            future.get();
        }

        // 按指定序执行
        List<MyInterface> orderedImplementations = new ArrayList<>();
        orderedImplementations.add(new MyImplementation3());
        orderedImplementations.add(new MyImplementation1());
        orderedImplementations.add(new MyImplementation2());

        for (MyInterface implementation : orderedImplementations) {
            implementation.execute();
        }

        // 支持分类执行
        List<MyInterface> category1Implementations = new ArrayList<>();
        List<MyInterface> category2Implementations = new ArrayList<>();

        // 根据分类添加实现类到相应列表中
        category1Implementations.add(new MyImplementation1());
        category1Implementations.add(new MyImplementation2());
        category2Implementations.add(new MyImplementation3());

        // 分别执行分类列表中的实现类
        for (MyInterface implementation : category1Implementations) {
            implementation.execute();
        }

        for (MyInterface implementation : category2Implementations) {
            implementation.execute();
        }

        // 关闭线程池
        executorService.shutdown();
    }
}

在上述示例代码中,首先定义了一个MyInterface接口,并创建了几个实现了该接口的具体实现类(MyImplementation1MyImplementation2MyImplementation3)。

接着,在main方法中,我们可以看到以下功能:

  • 同步执行:使用for循环依次执行接口实现列表中的每个实现类的execute()方法。
  • 异步执行:通过创建固定大小的线程池(ExecutorService)和Future对象列表,使用executorService.submit方法将每个实现类的execute()方法提交给线程池执行,并使用future.get()等待所有异步任务完成。
  • 按指定序执行:创建一个按指定序的实现类列表(orderedImplementations),并使用for循环按顺序执行每个实现类的execute()方法。
  • 支持分类执行:创建不同的分类实现类列表(category1Implementationscategory2Implementations),并使用for循环分别执行每个分类列表中的实现类的execute()方法。

最后,示例代码关闭了线程池(executorService.shutdown())。

你可以根据自己的需求,替换接口类和具体实现类,调整执行顺序,以及添加更多的分类执行方式。

举报

相关推荐

0 条评论